我想在尚未合并到主分支的所有更改的差异。

我试着:

git diff master
git diff branch..master
git diff branch...master

但是,在每一种情况下,差异在master中包含的内容还没有合并到我的分支中。

是否有一种方法可以在我的分支和master之间做一个区别,排除master中还没有合并到我的分支中的更改?


当前回答

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

合并基点是分支与主分支分离的点。

Git diff支持一种特殊的语法:

git diff master...branch

你不能交换两边因为那样你会得到另一个分支。你想知道的是分支在与主分支分离后发生了什么变化,而不是相反。

你可能想用HEAD替换这个语法中的branch,甚至完全删除它——下面这两种方法都显示了当前分支的内容,因为它与master分离了:

git diff master...HEAD
git diff master...

松散相关:

用Git找到一个分支点? 我怎样才能看到另一个分支是从哪个分支分叉的?


注意..和…语法不具有与其他Git工具相同的语义。它不同于man gitrevisions所指定的意思。

引用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 using HEAD 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 using HEAD instead. Just in case 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 in gitrevisions[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 in gitrevisions[7].

其他回答

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

合并基点是分支与主分支分离的点。

Git diff支持一种特殊的语法:

git diff master...branch

你不能交换两边因为那样你会得到另一个分支。你想知道的是分支在与主分支分离后发生了什么变化,而不是相反。

你可能想用HEAD替换这个语法中的branch,甚至完全删除它——下面这两种方法都显示了当前分支的内容,因为它与master分离了:

git diff master...HEAD
git diff master...

松散相关:

用Git找到一个分支点? 我怎样才能看到另一个分支是从哪个分支分叉的?


注意..和…语法不具有与其他Git工具相同的语义。它不同于man gitrevisions所指定的意思。

引用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 using HEAD 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 using HEAD instead. Just in case 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 in gitrevisions[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 in gitrevisions[7].

以下是对我有效的方法:

git diff origin/master...

这只显示了我当前选择的本地分支和远程主分支之间的更改,而忽略了来自合并提交的本地分支中的所有更改。

把分支/commitIDs之间的git差异放到一个补丁文件中(只有在没有删除或创建文件的情况下才可以),然后应用到新的分支中。

git checkout -b new-branch-name
git diff abcde123..branch-name > branch-name.patch
git apply branch-name.patch
rm branch-name.patch
git add .
git commit -m '#123 commit msg’
git push

其中abcde123是master中的提交ID,其中还没有不必要的更改。

基本上,你不需要master。 你可以将当前提交与当前分支中的任何提交进行比较,例如在你开始更改之前的最后一次提交。

git diff a4488d20406a1f26888d80eb7d57aa3b0054e307
or
git diff 553e4c7dcdcf366917d36ad8f28a273c88d92a1b..a4488d20406a1f26888d80eb7d57aa3b0054e307

根据文档

git diff显示工作树和索引之间的变化 树,索引和树之间的变化,两棵树之间的变化, 合并导致的更改、两个blob对象之间的更改或 磁盘上两个文件之间的变化。

In git diff -两点之间有很大的不同。还有3个点…在我们的存储库中比较分支或提取请求的方式中。我给你们举个简单的例子。

示例:让我们假设我们正在从master签出新的分支并将一些代码推入。

  G---H---I feature (Branch)
 /
A---B---C---D master (Branch)

两个点-如果我们想要显示两边当前时间内发生的所有变化之间的差异,我们将使用git diff origin/master..特征或只是git差异的起源/master ,输出:(H, I对A, B, C, D) 三个点-如果我们想要显示最后一个公共祖先(A)之间的差异,也就是我们开始新分支的检查点,我们使用git diff origin/master…特征,输出:(H, I对A)。 大多数情况下我宁愿用3个点。

正如John Szakmeister和VasiliNovikov所指出的,从master的角度获得你的分支的全部差异的最短命令是:

git diff master...

这将使用本地的master副本。

要比较特定文件的使用情况:

git diff master... filepath

输出的例子: