git diff 仅显示修订/分支 A 领先于修订/分支 B 的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17605208/
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-10 16:32:36  来源:igfitidea点击:

git diff showing only commits that revision/branch A is ahead of revision/branch B

git

提问by mwhite

Sometimes I have the following problem:

有时我会遇到以下问题:

  1. Add some commits in a feature branch.

  2. Update master from upstream.

  3. Want to see the diff between the feature branch and master, but git diff mastershows all of the things that have been added/removed in master, when I really only want to see the commits that the feature branch is ahead of master, not the ones that it's behind.

  1. 在功能分支中添加一些提交。

  2. 从上游更新 master。

  3. 想要查看功能分支和 master 之间的差异,但git diff master显示在 master 中添加/删除的所有内容,当我真的只想看到功能分支领先于 master 的提交,而不是它所在的提交在后面。

Is there a way to do this without having to merge master into the feature branch?

有没有办法在不必将 master 合并到 feature 分支的情况下做到这一点?

回答by Danny Roberts

I think the right answer is triple dot

我认为正确的答案是三点

git diff master...feature

That shows only new changes on feature with respect to master. See also: git diff - show only what's new on the remote

这仅显示了与 master 相关的功能上的新更改。另请参阅:git diff - 仅显示遥控器上的新内容

回答by Peter Lundgren

I usually go for

我通常去

git log -p feature-branch --not master

But if you want a diff, you're looking for

但如果你想要一个差异,你正在寻找

git diff <common-ancestor> master

where <common-ancestor>is a common ancestor of your feature branch and master that you want to compare to. You can use merge-baseto find that for you.

<common-ancestor>您要比较的功能分支和 master 的共同祖先在哪里。你可以用它merge-base来为你找到。

git diff `git merge-base feature-branch master` feature-branch

回答by Peter Lundgren

Your question is a little unclear because I'm not sure if you want to see a diff/patch of the changes between masterand feature, or you just want to see which branches contain which commits that the other branch doesn't have.

您的问题有点不清楚,因为我不确定您是否想查看master和之间更改的差异/补丁feature,或者您只想查看哪些分支包含另一个分支没有的提交。

Assuming that you just want to see what commits are in your featurebranch that don't exist in master(i.e. which commits in featureare "ahead" of master), then you can do

假设您只想查看feature分支中不存在的master提交(即哪些提交feature“领先”于master),那么您可以这样做

$ git log --oneline --graph master..feature

If you want to see how both masterand featurehave diverged, you can use this command:

如果你想看看这两个masterfeature有分歧,你可以使用这个命令:

$ git log --oneline --graph --first-parent \
--decorate --left-right master...feature
> 5bef654 (feature) A ...
> f7c65ea B ...
> fc66245 C ...
> 7435fc0 D ...
< bf68204 (master) X ...
< 0c10ed1 Y ...
< 27bb774 Z ...

The above output shows commits in masterthat are not in featurewith <in front of them (since you used master...featurewith master on the left <side of the command), while commits in featurethat aren't in master are marked with >since you used featureon the right side of master...feature. The triple dots ...in this form are important, don't leave them out.

在以上显示的提交master不在feature<在他们面前(因为你使用master...feature与主左侧<的命令的一侧),而在提交feature不在主标有>,因为你使用feature上的右侧master...feature. ...这种形式的三个点很重要,不要遗漏它们。

You can learn more about specifying Commit Range from the Pro Git book.

您可以从 Pro Git 书籍中了解有关指定提交范围的更多信息。

回答by gtrig

If I understand your question correctly, your feature branch is off of the master branch, and you want to see the differences between what you have committed on the feature branch and what the master branch looked like when you branched from it.

如果我正确理解了您的问题,那么您的功能分支不在 master 分支之外,并且您想查看您在功能分支上提交的内容与从它分支时 master 分支的外观之间的差异。

A----B----C
 \
  D----E----F

In that case you can use use the git diffcommand with the corresponding hashes of the commits. So for a feature branch that branched at point A and has subsequently committed D, E, and F, you can do this:

在这种情况下,您可以使用git diff带有相应提交哈希的命令。因此,对于在 A 点分支并随后提交 D、E 和 F 的功能分支,您可以执行以下操作:

git diff <hash of commit F> <hash of commit A>

You can use git merge-base feature masterto see the hash of point A to use above, and git logto see the hash for your most recent commit, F, on the feature branch. Or to do it all on one line use this command:

您可以使用git merge-base feature master来查看要在上面使用的点 Agit log的哈希值,以及在功能分支上查看最近提交 F 的哈希值。或者要在一行上完成所有操作,请使用以下命令:

git diff feature `git merge-base feature master`

回答by akras14

Not an exact answer, but a one liner that works most of the time for my needs.

不是一个确切的答案,而是一个在大部分时间都可以满足我的需求的衬垫。

git difftool -d master..

git difftool -d master..