如何查看Git提交中的更改?

2263

当我执行git diff COMMIT命令时,我看到的是该提交和HEAD之间的更改(就我所知),但我想要查看单个提交所做的更改。

我没有在diff/log上找到任何明显的选项可以给我这个输出。


9
可能是Shorthand for diff of git commit with its parent?的重复。 - Chris Maes
26个回答

4
这个命令将获取Git父commit的哈希值:
git log -n 2 <commit-hash>

之后执行git diff-tool <commit-hash> <parent-commit-hash>

例子:

bonnie@bonnie ~/ $ git log -n 2 7f65b9a9d3820525766fcba285b3c678e889fe3

commit 7f65b9a9d3820525766fcba285b3c678e889fe3b
Author: souparno <souparno.majumder@gmail.com>
Date:   Mon Jul 25 13:17:07 2016 +0530

CSS changed to maintain the aspect ratio of the channel logos and to fit them properly.

commit c3a61f17e14e2b80cf64b172a45f1b4826ee291f
Author: souparno <souparno.majumder@gmail.com>
Date:   Mon Jul 25 11:28:09 2016 +0530

The ratio of the height to width of the channel images are maintained.

在此之后
git difftool 7f65b9a9d3820525766fcba285b3c678e889fe3b c3a61f17e14e2b80cf64b172a45f1b4826ee291f

4
我正在Windows 10上运行Git版本2.6.1.windows.1,因此我需要对Nevik的答案进行微小修改(使用波浪号而不是插入符):
git diff COMMIT~ COMMIT

另一种选择是引用插入符号:
git diff "COMMIT^" COMMIT

4

获取提交中更改的文件列表:

git show --name-only commit_id

注意:上述命令不适用于合并 ID。


获取合并提交ID中更改的文件列表:

git log -m -1 --name-only commit_id


查看提交中特定文件的更改: git show commit_id:src/path/to/that/file


жңҖеҘҪдҪҝз”Ёgit show commit_id -- src/path/to/that/fileе‘Ҫд»ӨжқҘиҺ·еҸ–еҪ©иүІиҫ“еҮәгҖӮ - Eric

3

还可以查看特定文件在两个提交之间的更改记录。

git diff <commit_Id_1> <commit_Id_2> some_dir/file.txt

git diff <commit_Id> on/specific/file.txt - CallMarl

2

如果需要以图形化视图检查源更改,请使用以下命令:gitk

gitk (your commit id goes here)

例如:

gitk HEAD~1 

我不明白为什么这个答案被踩了。我同意命令行和基于文本的工具是正确的选择,但gitk提供了很多有用的信息。 - ShellFish
2
gitk 不是 git,它是一个特定的软件包,在安装了 git 的每个操作系统上都不存在。tig 也是很好的工具之一,还有许多其他工具可供选择。 - CallMarl

-5
  1. 您可以点击Git的特定地址上的每个提交以查看
  2. 如果您使用工具提交,您可以通过传递show history来显示历史记录

目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community
这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - abdo Salm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接