在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:35:12  来源:igfitidea点击:

In Git, how do I diff to the first commit of my branch?

gitgit-diff

提问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~6if 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-basecommand prints the "best" (most recent) common ancestor, so the above command shows the difference from the most recent commit that HEADhas in common with otherbranchto HEAD.

merge-base命令打印“最佳”(最近的)共同祖先,因此上面的命令显示了HEAD与与otherbranchto相同的最近提交的差异HEAD

Note you can use @{u}in place of otherbranchto see the changes you made since you diverged from the upstream branch. See git help revisionsfor 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.