如何按日期在 Git 中结帐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6990484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to checkout in Git by date?
提问by Amir Afghani
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?
我正在研究源代码中的回归。我想告诉 Git:“根据参数化的日期/时间检查源代码”。这可能吗?
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.
我也对我目前的观点进行了一些我不想失去的改变。理想情况下,我想在当前源和我基于以前日期感兴趣的某个版本之间来回切换。
回答by Andy
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 carleetosaid) git commit
it to a separate branch.
您可以使用git stash
. 你会用git stash pop
它来取回它。或者你可以(如carleeto所说)将git commit
它放到一个单独的分支。
Checkout by date using rev-parse
使用 rev-parse 按日期结帐
You can checkout a commit by a specific date using rev-parse
like this:
您可以使用rev-parse
如下方式在特定日期签出提交:
git checkout 'master@{1979-02-26 18:30:00}'
More details on the available options can be found in the git-rev-parse
.
有关可用选项的更多详细信息,请参见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.
如评论中所述,此方法使用 reflog 来查找历史记录中的提交。默认情况下,这些条目会在 90 天后过期。尽管使用 reflog 的语法不那么冗长,但您只能返回 90 天。
Checkout out by date using rev-list
使用 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:
另一个不使用 reflog 的选项是用于rev-list
在特定时间点获取提交:
git checkout `git rev-list -n 1 --first-parent --before="2009-07-27 13:37" master`
Note the --first-parentif you want only your history and not versions brought in by a merge. That's what you usually want.
如果您只想要历史记录而不是合并带来的版本,请注意--first-parent。这就是你通常想要的。
回答by Rocky
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`
回答by Carl
Looks like you need something along the lines of this: Git checkout based on date
看起来你需要一些类似的东西: Git checkout based on date
In other words, you use rev-list
to find the commit and then use checkout to
actually get it.
换句话说,您使用rev-list
来查找提交,然后使用 checkout 来实际获取它。
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`
回答by Steven Penny
To those who prefer a pipe to command substitution
对于那些喜欢用管道代替命令的人
git rev-list -n1 --before=2013-7-4 master | xargs git checkout
回答by BartoszKP
In my case the -n 1
option doesn't work. On Windows I've found that the following sequence of commands works fine:
在我的情况下,该-n 1
选项不起作用。在 Windows 上,我发现以下命令序列可以正常工作:
git rev-list -1 --before="2012-01-15 12:00" master
This returns the appropriate commit's SHA for the given date, and then:
这将返回给定日期的适当提交的 SHA,然后:
git checkout SHA
回答by Diomidis Spinellis
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 rev-parse
如果您感兴趣的日期是提交日期,@Andy 提出的解决方案可以正常工作。但是,如果您想根据作者的日期结帐,rev-parse
则行不通,因为它不提供使用该日期来选择提交的选项。相反,您可以使用以下内容。
git checkout $(
git log --reverse --author-date-order --pretty=format:'%ai %H' master |
awk '{hash = } >= "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 awkpredicate.)
(如果您还想$1 >= "2016-04-12" && $2 >= "11:37"
在awk谓词中指定时间使用。)
回答by egerlach
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):
进一步使用该rev-list
选项,如果您想找到从主分支到生产分支的最新合并提交(作为一个纯粹的假设示例):
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.
我需要找到截至给定日期在生产服务器上的代码。这为我找到了。
回答by antlersoft
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.
其他答案提供了在某个时间将存储库返回到分支中最近提交的技术——但它们可能并不总是足够的。例如,如果您从一个分支构建,然后删除该分支,或者从一个后来重新定位的分支构建,那么您构建的提交在任何当前分支的 git 中都可能变得“无法访问”。当存储库被压缩时,git 中无法访问的对象最终可能会被删除。
Putting a tag on the commit means it never becomes unreachable, no matter what you do with branches afterwards (barring removing the tag).
将标记放在提交上意味着它永远不会变得无法访问,无论您之后对分支做什么(除非删除标记)。
回答by Luca C.
git rev-list -n 1 --before="2009-07-27 13:37" origin/master
take the printed string (for instance XXXX) and do:
获取打印的字符串(例如 XXXX)并执行以下操作:
git checkout XXXX