当前分支和 master 之间的 Git 差异,但不包括未合并的 master 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20808892/
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
Git diff between current branch and master but not including unmerged master commits
提问by pillarOfLight
I want a diff of all changes in a branch that is not merged to master yet.
我想要一个尚未合并到 master 的分支中所有更改的差异。
I tried:
我试过:
git diff master
git diff branch..master
git diff branch...master
However, in each of these cases the diff contains content in master that has not been merged into my branch yet.
但是,在每种情况下,差异都包含 master 中尚未合并到我的分支中的内容。
Is there a way to do a diff between my branch and master that excludes changes in master that have not been merged into my branch yet?
有没有办法在我的分支和 master 之间做一个差异,排除 master 中尚未合并到我的分支中的更改?
回答by Palec
git diff `git merge-base master branch`..branch
Merge baseis the point where branch
diverged from master
.
合并基点是branch
从master
.
Git diff supports a special syntax for this:
Git diff 为此支持一种特殊的语法:
git diff master...branch
You must not swap the sides because then you would get the other branch. You want to know what changed in branch
since it diverged from master
, not the other way round.
你不能交换两边,因为那样你会得到另一个分支。您想知道branch
自 偏离 后发生了什么变化master
,而不是相反。
Loosely related:
松散的相关资料:
Note that ..
and ...
syntax does not have the same semantics as in other Git tools. It differs from the meaning specified in man gitrevisions
.
请注意,..
and...
语法与其他 Git 工具的语义不同。它与 中指定的含义不同man gitrevisions
。
Quoting man git-diff
:
引用man git-diff
:
git diff [--options] <commit> <commit> [--] [<path>…]
This is to view the changes between two arbitrary
<commit>
.
git diff [--options] <commit>..<commit> [--] [<path>…]
This is synonymous to the previous form. If
<commit>
on one side is omitted, it will have the same effect as usingHEAD
instead.
git diff [--options] <commit>...<commit> [--] [<path>…]
This form is to view the changes on the branch containing and up to the second
<commit>
, starting at a common ancestor of both<commit>
. "git diff A...B
" is equivalent to "git diff $(git-merge-base A B) B
". You can omit any one of<commit>
, which has the same effect as usingHEAD
instead.Just in case if you are doing something exotic, it should be noted that all of the
<commit>
in the above description, except in the last two forms that use ".." notations, can be any<tree>
.For a more complete list of ways to spell
<commit>
, see "SPECIFYING REVISIONS" section ingitrevisions[7]
. However, "diff" is about comparing two endpoints, not ranges, and the range notations ("<commit>..<commit>
" and "<commit>...<commit>
") do not mean a range as defined in the "SPECIFYING RANGES" section ingitrevisions[7]
.
git diff [--options] <commit> <commit> [--] [<path>…]
这是为了查看两个随心所欲的变化
<commit>
。
git diff [--options] <commit>..<commit> [--] [<path>…]
这与前面的形式同义。如果
<commit>
在一侧省略,则与使用HEAD
相反的效果相同。
git diff [--options] <commit>...<commit> [--] [<path>…]
此表单用于查看包含和直到第二个分支上的更改
<commit>
,从两者的共同祖先开始<commit>
。“git diff A...B
”相当于“git diff $(git-merge-base A B) B
”。您可以省略其中的任何一个<commit>
,这与使用的效果相同HEAD
。以防万一,如果您正在做一些奇特的事情,应该注意的是
<commit>
,除了最后两种使用“..”符号的形式之外,上面描述中的所有内容都可以是任何<tree>
.有关拼写方法的更完整列表
<commit>
,请参阅 中的“指定修订”部分gitrevisions[7]
。但是,“diff”是关于比较两个端点,而不是范围,范围符号(“<commit>..<commit>
” 和“<commit>...<commit>
”)并不表示gitrevisions[7]
.
回答by Jeshurun
Here's what worked for me:
以下是对我有用的内容:
git diff origin/master...
This shows only the changes between my currently selected local branch and the remote master branch, and ignores all changes in my local branch that came from merge commits.
这仅显示我当前选择的本地分支和远程主分支之间的更改,并忽略来自合并提交的本地分支中的所有更改。
回答by Andrew Schreiber
As also noted by John Szakmeister and VasiliNovikov, the shortest command to get the full diff from master's perspective on your branch is:
正如 John Szakmeister 和 VasiliNovikov 所指出的,从 master 的角度在您的分支上获得完整差异的最短命令是:
git diff master...
This uses your local copy of master.
这将使用您的本地主副本。
To compare a specific file use:
要比较特定文件,请使用:
git diff master... filepath
Output example:
输出示例: