即使每个更改的文件都与其中一个父文件一致,如何“git show”合并提交并合并差异输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5072693/
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
How to "git show" a merge commit with combined diff output even when every changed file agrees with one of the parents?
提问by Tilman Vogel
After doing a "simple" merge (one without conflicts), git show
usually only shows something like
在做了一个“简单”的合并(没有冲突的)之后,git show
通常只显示类似的东西
commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)
Merge: fc17405 ee2de56
Author: Tilman Vogel <email@email>
Date: Tue Feb 22 00:27:17 2011 +0100
Merge branch 'testing' into master
This is because, for merges, git show
uses the combined diff format which omits files that agree with either of the parent versions.
这是因为,对于合并,git show
使用组合差异格式忽略与任一父版本一致的文件。
Is there a way to force git to still show all differences in combined diff mode?
有没有办法强制 git 在组合差异模式下仍然显示所有差异?
Doing git show -m
will show the differences (using pairwise diffs between the new and all parent versions respectively) but I would prefer to have that with the differnces marked by +/- in the respective columns like in combined mode.
这样做git show -m
将显示差异(分别使用新版本和所有父版本之间的成对差异),但我更喜欢在组合模式中在各自的列中用 +/- 标记差异。
采纳答案by apenwarr
No, there is no way to do this with git show
. But it would certainly be nice sometimes, and it would probably be relatively easy to implement in the git source code (after all, you just have to tell it to nottrim out what it thinks is extraneous output), so the patch to do so would probably be accepted by the git maintainers.
不,没有办法做到这一点git show
。但有时它肯定会很好,并且在 git 源代码中实现它可能相对容易(毕竟,你只需要告诉它不要修剪掉它认为无关的输出),所以补丁这样做可能会被 git 维护者接受。
Be careful what you wish for, though; merging a branch with a one-line change that was forked three months ago will still have a hugediff versus the mainline, and so such a full diff would be almost completely unhelpful. That's why git doesn't show it.
不过要小心你想要什么;将一个分支与三个月前分叉的单行更改合并,与主线相比仍然会有巨大的差异,因此这样一个完整的差异几乎完全没有帮助。这就是为什么 git 不显示它。
回答by rip747
Look at the commit message:
查看提交消息:
commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)
Merge: fc17405 ee2de56
Author: Tilman Vogel <email@email>
Date: Tue Feb 22 00:27:17 2011 +0100
Merge branch 'testing' into master
notice the line:
注意这一行:
Merge: fc17405 ee2de56
take those two commit ids and reverse them. so in order get the diff that you want, you would do:
获取这两个提交 ID 并反转它们。所以为了得到你想要的差异,你会做:
git diff ee2de56..fc17405
to show just the names of the changed files:
仅显示已更改文件的名称:
git diff --name-only ee2de56..fc17405
and to extract them, you can add this to your gitconfig:
并提取它们,您可以将其添加到您的 gitconfig 中:
exportfiles = !sh -c 'git diff git exportfiles ee2de56..fc17405 /c/temp/myproject
--name-only | "while read files; do mkdir -p \"/$(dirname $files)\"; cp -vf $files /$(dirname $files); done"'
then use it by doing:
然后通过执行以下操作使用它:
git diff fc17405...ee2de56
回答by CodeManX
A better solution (mentioned by @KrisNuttycombe):
更好的解决方案(@KrisNuttycombe 提到):
commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)
Merge: fc17405 ee2de56
Author: Tilman Vogel <email@email>
Date: Tue Feb 22 00:27:17 2011 +0100
for the merge commit:
对于合并提交:
git diff fc17405...ee2de56 --name-only
to show all of the changes on ee2de56
that are reachable from commits on fc17405
. Note the order of the commit hashes - it's the same as shown in the merge info: Merge: fc17405 ee2de56
显示ee2de56
可从提交上访问的所有更改fc17405
。请注意提交哈希的顺序 - 它与合并信息中显示的相同:Merge: fc17405 ee2de56
Also note the 3 dots ...
instead of two!
还要注意 3 个点...
而不是两个点!
For a list of changed files, you can use:
有关已更改文件的列表,您可以使用:
git merge --squash testing
回答by side2k
You can create branch with HEAD set to one commit before merge. Then, you can do:
您可以在合并前将 HEAD 设置为一次提交来创建分支。然后,你可以这样做:
git diff
This will merge, but not commit. Then:
这将合并,但不会提交。然后:
git diff HEAD^ HEAD^2
回答by patthoyts
I think you just need 'git show -c $ref'. Trying this on the git repository on a8e4a59 shows a combined diff (plus/minus chars in one of 2 columns). As the git-show manual mentions, it pretty much delegates to 'git diff-tree' so those options look useful.
我认为你只需要'git show -c $ref'。在 a8e4a59 上的 git 存储库上尝试此操作会显示组合差异(两列之一中的加/减字符)。正如 git-show 手册所提到的,它几乎委托给“git diff-tree”,因此这些选项看起来很有用。
回答by max630
Seems like answered here: http://thread.gmane.org/gmane.comp.version-control.git/191553/focus=191557
似乎在这里回答:http: //thread.gmane.org/gmane.comp.version-control.git/191553/focus=191557
So in a similar way, running
$ git diff --cc $M $M^1 $M^2 $(git merge-base $M^1 $M^2)
should show a combined patch that explains the state at $M relative to the states recorded in its parents and the merge base.
所以以类似的方式,运行
$ git diff --cc $M $M^1 $M^2 $(git merge-base $M^1 $M^2)
应该显示一个组合补丁,解释 $M 处的状态相对于其父级和合并基础中记录的状态。
回答by gurugray
in your case you just need to
在你的情况下,你只需要
git diff 0e1329e55^ 0e1329e55^2
or just hash for you commit:
或者只是为你提交哈希:
git diff 0e1329e5^..0e1329e5
回答by hesham_EE
If your merge commit is commit 0e1329e5, as above, you can get the diff that was contained in this merge by:
如果您的合并提交是提交 0e1329e5,如上所述,您可以通过以下方式获取此合并中包含的差异:
git diff-tree -c {merged_commit_sha}
I hope this helps!
我希望这有帮助!
回答by Bruce Dawson
If you are sitting at the merge commit then this shows the diffs:
如果您坐在合并提交中,那么这将显示差异:
git diff HEAD~1..HEAD
git diff HEAD~1..HEAD
If you're not at the merge commit then just replace HEAD with the merge commit. This method seems like the simplest and most intuitive.
如果您不在合并提交中,则只需将 HEAD 替换为合并提交。这种方法似乎是最简单、最直观的。
回答by Ehsan Mirsaeedi
You can use the diff-tree command with the -c flag. This command shows you what files have changed in the merge commit.
您可以使用带有 -c 标志的 diff-tree 命令。此命令显示在合并提交中更改了哪些文件。
[alias]
range = "!. ~/.githelpers && run_on_merge_range"
I got the -c flag's description from Git-Scm:
我从Git-Scm得到了 -c 标志的描述:
This flag changes the way a merge commit is displayed (which means it is useful only when the command is given one , or --stdin). It shows the differences from each of the parents to the merge result simultaneously instead of showing pairwise diff between a parent and the result one at a time (which is what the -m option does). Furthermore, it lists only files which were modified from all parents.
这个标志改变了合并提交的显示方式(这意味着它只有在给定一个命令或 --stdin 时才有用)。它同时显示每个父级与合并结果的差异,而不是一次显示一个父级和结果之间的成对差异(这是 -m 选项的作用)。此外,它仅列出所有父项修改过的文件。
回答by Nerdmaster
I built a general-purpose approach to doing various operations on a merge's commits.
我构建了一种通用方法来对合并的提交执行各种操作。
Step One: Add an alias to git by editing ~/.gitconfig
:
第一步:通过编辑为 git 添加别名~/.gitconfig
:
run_on_merge_range() {
cmd=; shift
commit=; shift
range=$(git show $commit | grep Merge: | awk '{print "..." }')
echo "git $cmd $range $@"
if [ -z $range ]; then
echo "No merge detected"
exit 1
fi
git $cmd $range $@
}
Step Two: In ~/.githelpers
, define a bash function:
第二步:在 中~/.githelpers
,定义一个 bash 函数:
git range log <merge SHA> --oneline
git range diff <merge SHA> --reverse -p
git range diff <merge SHA> --name-only
Step Three: Profit!
第三步:盈利!
##代码##There is probably a LOT of room for improvement here, I just whipped this together to get past an annoying situation. Feel free to mock my bash syntax and/or logic.
这里可能有很多改进的空间,我只是把它放在一起以克服烦人的情况。随意嘲笑我的 bash 语法和/或逻辑。