Amir Afghani Amir Afghani - 10 months ago 81
Git Question

git checkout 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 --before="2009-07-27 13:37" master`