参考 git 中的上一个/下一个提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16062358/
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
Referring to the previous/next commit in git?
提问by HelloGoodbye
I have seen git commands use a syntax such as HEAD~
, but I haven't been able to find this syntax in the Git Reference Manual.
我见过 git 命令使用诸如 的语法HEAD~
,但我在 Git参考手册 中找不到这种语法。
Here is what I have understood: <commit>~<n>
refers to the commit <n>steps earlier than <commit>(where <n>is an integer number), and commit~
simply means the same and that <n>implicitly is one.
这是我所理解的:<commit>~<n>
指的是<commit>之前的提交<n>步骤(其中<n>是一个整数),并且仅表示相同且<n>隐式为 1。commit~
Now, is this correct? In that case, does this always work? What if <commit>is the result of a merge between two branches, which commit will then <commit>~
refer to? Is there some corresponding syntax for referring to the next commit or the commit <n>steps later?
现在,这是正确的吗?在那种情况下,这总是有效吗?如果<commit>是两个分支之间合并的结果,然后将<commit>~
引用哪个提交呢?是否有一些相应的语法用于引用下一次提交或稍后的提交<n>步骤?
回答by cexbrayat
You have a very clear explanation of how this works in the chapter on Acenstry References in Pro Git:
在 Pro Git 中的 Acenstry References一章中,您对它的工作原理有非常清楚的解释:
~
is used to get the first parent.^
can be used to get the other parents (^2
, for example, for a merge).
~
用于获取第一个父级。^
可用于获取其他父级(^2
例如,用于合并)。
But you don't have a simple way to reference the next commit, even if there are more convoluted waysto get it.
但是你没有一个简单的方法来引用下一个提交,即使有更复杂的方法来获取它。
回答by Hubert OG
To simply answer the question from title (since that's what got me here from Google):
简单地回答标题中的问题(因为那是我从 Google 来到这里的原因):
To checkout the previous commit:
检出之前的提交:
git checkout HEAD^
To checkout the next commit (assuming there's no branching):
要检查下一次提交(假设没有分支):
git checkout `git log --reverse --ancestry-path HEAD..master | head -n 1 | cut -d \ -f 2`
回答by Jon Carter
Inspired by @cexbrayat's answer, I find it useful to think of it this way:
受到@cexbrayat 回答的启发,我发现这样思考很有用:
How to refer to something in a commit's ancestry, where a commit can have multiple parents:
如何引用提交的祖先中的某些内容,其中提交可以有多个父项:
^n
specifies which parent~n
specifies which generation
^n
指定哪个父级~n
指定哪一代
Both default to one.
两者都默认为一。