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.
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.
Andy's solution does not work for me. Here I found another way:
git checkout `git rev-list -n 1 --before="2009-07-27 13:37" master`
Git: checkout by date
Looks like you need something along the lines of this: Git checkout based on date
In other words, you use rev-list
to find the commit and then use checkout to
actually get it.
If you don't want to lose your staged changes, the easiest thing would be to create a new branch and commit them to that branch. You can always switch back and forth between branches.
Edit: The link is down, so here's the command:
git checkout `git rev-list -n 1 --before="2009-07-27 13:37" master`