问题
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.
回答1:
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.
回答2:
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
回答3:
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`
回答4:
To those who prefer a pipe to command substitution
git rev-list -n1 --before=2013-7-4 master | xargs git checkout
回答5:
In my case the -n 1
option doesn't work. On Windows I've found that the following sequence of commands works fine:
git rev-list -1 --before="2012-01-15 12:00" master
This returns the appropriate commit's SHA for the given date, and then:
git checkout SHA
回答6:
Going further with the rev-list
option, if you want to find the most recent merge commit from your master branch into your production branch (as a purely hypothetical example):
git checkout `git rev-list -n 1 --merges --first-parent --before="2012-01-01" production`
I needed to find the code that was on the production servers as of a given date. This found it for me.
回答7:
The git rev-parse
solution proposed by @Andy works fine if the date you're interested is the commit's date. If however you want to checkout based on the author's date, rev-parse
won't work, because it doesn't offer an option to use that date for selecting the commits. Instead, you can use the following.
git checkout $(
git log --reverse --author-date-order --pretty=format:'%ai %H' master |
awk '{hash = $4} $1 >= "2016-04-12" {print hash; exit 0 }
)
(If you also want to specify the time use $1 >= "2016-04-12" && $2 >= "11:37"
in the awk predicate.)
回答8:
If you want to be able to return to the precise version of the repository at the time you do a build it is best to tag the commit from which you make the build.
The other answers provide techniques to return the repository to the most recent commit in a branch as of a certain time-- but they might not always suffice. For example, if you build from a branch, and later delete the branch, or build from a branch that is later rebased, the commit you built from can become "unreachable" in git from any current branch. Unreachable objects in git may eventually be removed when the repository is compacted.
Putting a tag on the commit means it never becomes unreachable, no matter what you do with branches afterwards (barring removing the tag).
回答9:
git rev-list -n 1 --before="2009-07-27 13:37" origin/master
take the printed string (for instance XXXX) and do:
git checkout XXXX
来源:https://stackoverflow.com/questions/6990484/how-to-checkout-in-git-by-date