在 Git 中,如何区分我的分支的第一次提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6391128/
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
In Git, how do I diff to the first commit of my branch?
提问by Newtang
I've made several commits to a local branch, but I'm not sure what the best way to diff what I currently have to my branch's starting state. I know I can do something like git diff HEAD HEAD~6
if there's been 6 commits to my branch, but I was hoping for something that was independent of the number of commits.
我已经对本地分支进行了几次提交,但是我不确定将我目前拥有的内容与我的分支的起始状态进行比较的最佳方法是什么。我知道我可以做一些事情,比如git diff HEAD HEAD~6
我的分支有 6 次提交,但我希望有一些与提交次数无关的事情。
Edit: I failed to mention this: I was hoping I wouldn't have to dig through the log to get the hash of the commit I branched from. If I had 80 commits for example, this wouldn't be a fun task.
编辑:我没有提到这一点:我希望我不必通过日志来获取我分支的提交的哈希值。例如,如果我有 80 次提交,这将不是一项有趣的任务。
Also, presume that the original branch I branched from has already had several changes.
另外,假设我分支的原始分支已经发生了一些变化。
回答by Richard Hansen
You'll want to use the triple dot syntax described in git help diff
:
您将需要使用中描述的三点语法git help diff
:
git diff otherbranch...
This is the same as:
这与:
git diff otherbranch...HEAD
which is the same as:
这与:
git diff $(git merge-base otherbranch HEAD) HEAD
The merge-base
command prints the "best" (most recent) common ancestor, so the above command shows the difference from the most recent commit that HEAD
has in common with otherbranch
to HEAD
.
该merge-base
命令打印“最佳”(最近的)共同祖先,因此上面的命令显示了HEAD
与与otherbranch
to相同的最近提交的差异HEAD
。
Note you can use @{u}
in place of otherbranch
to see the changes you made since you diverged from the upstream branch. See git help revisions
for details about the syntax.
请注意,您可以使用@{u}
代替otherbranch
来查看自从您离开上游分支以来所做的更改。有关git help revisions
语法的详细信息,请参阅。
回答by vhallac
First, you need to find the commit you branched from. Once you have that, you can simply do a git diff <branch-point>..HEAD
(as yasouser pointed out).
首先,您需要找到您从. 一旦你有了它,你就可以简单地做一个git diff <branch-point>..HEAD
(正如 yasouser 指出的那样)。
回答by yasouser
git diff <SHA-1 of the commit from which you branched>..HEAD
git diff <SHA-1 of the commit from which you branched>..HEAD
You can get the SHA-1 of your branch point by doing a git log
.
您可以通过执行git log
.