gpt4 book ai didi

git - “git show” 不返回与 “git diff” 相同的结果

转载 作者:太空狗 更新时间:2023-10-29 13:40:54 26 4
gpt4 key购买 nike

我在 git 存储库中有一个提交,其中 git show 说只有一个文件被修改,而 git diff 显示两个文件被修改。

我创建了一个简单的示例存储库,它表现出相同的行为。

在示例存储库中,我们有这个简单的历史:

*   c248261      (HEAD, origin/master, master) Merged feature into master.
|\
| * d23c497 (feature) Modified fileA and fileB in feature.
* | 06a7f5e Modified fileA and fileC in master.
* | 9cd1a6e Merged feature into master.
|\ \
| |/
| * aed2e5e Modified fileA and fileB in feature.
* | c6e4fe7 Mofified fileC in master.
* | 19ed298 Merged feature to master.
|\ \
| |/
| * c0f2abc Added fileB and modified fileA in feature.
* | 47c67cf Added fileC in master.
|/
* 56a9b73 Added fileA in master.

例如,当我查看提交 c248261 时SourceTree,我可以看到 fileAfileB 被修改了。使用 git diff 得到相同的结果:

$ git diff --name-only c248261^..c248261
fileA
fileB
$

或使用速记符号:

$ git diff --name-only c248261^!
fileA
fileB
$

当我尝试使用 git show 获取相同的信息时,只显示了两个文件之一:

$ git show --name-only c248261
commit c2482616b6b6781d0580ec1008ef7d0ab5f73a70
Merge: 06a7f5e d23c497
Author: ...
Date: Fri Aug 15 16:19:02 2014 +0200

Merged feature into master.

fileA
$

同样,git diff-tree 什么也没显示:

$ git diff-tree c248261
$

有人能解释一下区别吗?

我使用的是 git 版本 2.0.4。

最佳答案

什么是git show

命令 git show 是一个通用命令,用于显示有关 git 中对象的信息。

来自手册页(git help show):

Shows one or more objects (blobs, trees, tags and commits).
...
For commits it shows the log message and textual diff.
It also presents the merge commit in a special format as produced by
git diff-tree --cc
...

因此,git show 的输出是不同的,因为这是一个merge 提交而不是一个plain 提交。

git diff-tree --cc 的手册页说:

--cc
This flag changes the way a merge commit patch is displayed, in a
similar way to the -c option. It implies the -c and -p options and
further compresses the patch output by omitting uninteresting hunks whose
the contents in the parents have only two variants and the merge result
picks one of them without modification. When all hunks are uninteresting,
the commit itself and the commit log message is not shown, just like in
any other "empty diff" case.

进一步

-c
This flag changes the way a merge commit is displayed (which means
it is useful only when the command is given one <tree-ish>, 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.

请注意我在这里重复的最后一行:

Furthermore, it lists only files which were modified from all parents.

因此 git show merge 提交将仅列出在将提交与 merge 的所有 父项进行比较时修改的文件。对于普通 merge (即 2 个父项),这正是被 merge 的文件。例如,如果您执行此 merge :

git checkout master
git merge feature

git show 列出的文件是在 masterfeature 之前修改的文件 merge 并在 merge 提交中 merge 在一起。

对于 Octopus merge ——即超过两个父项——生成的文件必须不同于 所有 父项中的文件才能被 git show 显示。

请注意,在 merge 提交中使用 git show 时,其他文件也可能会显示。例如,如果您调用 git merge --no-commit 然后在提交之前执行以下任何操作,那么这些文件也将被 git show 显示在 merge 提交:

  • 添加一个新文件。
  • 删除现有文件。
  • 修改任何文件——包括任何已经由 git merge 暂存的文件。

结论

git show 正在做的是显示一个所谓的组合差异

在 merge 提交上运行命令 git show 的结果可以被认为是只显示 merge 的文件,而不是简单地从两个父项之一中获取的未更改文件。

这有点令人惊讶,因为许多人希望看到与您刚刚 merge 到的分支相比哪些文件被修改了。

那么,如果您想查看 parent 双方的差异怎么办?

让我们最后看一下 git show 的手册页:

COMBINED DIFF FORMAT
Any diff-generating command can take the `-c` or --cc option to produce
a combined diff when showing a merge. This is the default format when
showing merges with git-diff(1) or git-show(1). Note also that you can
give the `-m' option to any of these commands to force generation of
diffs with individual parents of a merge.

所以我们可以使用 -m 选项来获得两个差异,如下所示:

git show -m commit

这将一一为您提供与所有 parent 的差异。

要查看 merge 提交与您 merge 到的分支(即第一个父级)上的先前提交之间的区别,请使用以下任一命令:

git show --first-parent commit
git diff commit^..commit
git diff commit^!

--first-parent 选项确实属于 git log 但也适用于 git show。符号 commit^!commit^..commit 的简写。

要查看 merge 提交和您 merge 的分支之间的区别(例如,查看您从其他分支遗漏的内容),使用

git diff commit^2..commit

符号 commit^2 表示 commit 的第二个父级。

如果您有超过 2 个父项(即 Octopus merge ),我相信您可以猜出如何与第 3、4 等提交进行比较。

关于git - “git show” 不返回与 “git diff” 相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com