Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to checkout in Git by date?

I am working on a regression in the source code. I'd like to tell Git: "checkout the source based on a parameterized date/time". Is this possible?

I also have staged changes in my current view that I don't want to lose. Ideally, I would like to toggle back and forth between the current source, and some version I'm interested in based on a previous date.

like image 877
Amir Afghani Avatar asked Aug 09 '11 01:08

Amir Afghani


People also ask

How do you checkout in git?

New BranchesGit checkout works hand-in-hand with git branch . The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

What does git checkout period do?

With branch specifier only ( git checkout branch ) it will switch current working directory to specified branch, keeping local changes if possible and failing otherwise. If you already are on branch , it will do nothing at all.

Does git checkout pull latest changes?

Indeed, git fetch pulls down the latest code from remote to origin/branch at local. If you have no local branch with the same name, then git checkout 2.1.


1 Answers

To keep your current changes

You can keep your work stashed away, without commiting it, with git stash. You would than use git stash pop to get it back. Or you can (as carleeto said) git commit it to a separate branch.

Checkout by date using rev-parse

You can checkout a commit by a specific date using rev-parse like this:

git checkout 'master@{1979-02-26 18:30:00}' 

More details on the available options can be found in the git-rev-parse.

As noted in the comments this method uses the reflog to find the commit in your history. By default these entries expire after 90 days. Although the syntax for using the reflog is less verbose you can only go back 90 days.

Checkout out by date using rev-list

The other option, which doesn't use the reflog, is to use rev-list to get the commit at a particular point in time with:

git checkout `git rev-list -n 1 --first-parent --before="2009-07-27 13:37" master` 

Note the --first-parent if you want only your history and not versions brought in by a merge. That's what you usually want.

like image 142
Andy Avatar answered Oct 14 '22 19:10

Andy