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

2263

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

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


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

23

另一种可能性:

git log -p COMMIT -1

16

我喜欢使用如下命令来比较一个特定的提交和它的上一个提交:

git diff <commit-hash>^-

例子:

git diff cd1b3f485^-

12

你可以使用 git diff HEAD HEAD^1 命令查看与父提交的差异。

如果你只想要查看文件列表,则需要添加 --stat 选项。


1
这是你的意思,git diff HEAD^1 HEAD - Shibir Basak
2
请注意,这将显示您添加的内容和移除的内容,因为它将进行反向比较。您应该阅读diff命令的方式是:我需要更改文件以从提交“HEAD”到提交“HEAD^1”? - brainplot

11
git difftool COMMIT^ <commit hash>

如果您已经配置了difftool,那么这也是可能的。
请参阅此处如何配置difftool。或手册页面此处
此外,您可以使用git diff-tree --no-commit-id --name-only -r <commit hash>来查看在给定的提交哈希中更改/提交的文件。

1
在我看来,git difftool -d 比普通的 git difftool 更易用。 - ks1322

10

要查看提交的作者和时间,请使用git show COMMIT。这将会得到类似下面的结果:

commit 13414df70354678b1b9304ebe4b6d204810f867e
Merge: a2a2894 3a1ba8f
Author: You <you@you.com>
Date:   Fri Jul 24 17:46:42 2015 -0700

     Merge remote-tracking branch 'origin/your-feature'

如果您想查看哪些文件已更改,请使用上面合并行中的值运行以下命令:git diff --stat a2a2894 3a1ba8f

如果您想查看实际差异,请运行git --stat a2a2894 3a1ba8f


如果您想查看实际差异,请运行 git --stat a2a2894 3a1ba8f。我认为您的意思是 git diff a2a2894 3a1ba8f,否则会出现 unknown option: --stat - 林果皞
git show COMMIT已经显示了普通提交的更改集,但对于合并提交不会显示。 - Mecki

9

检查完整的更改内容:

  git diff <commit_Id_1> <commit_Id_2>

检查仅更改/添加/删除的文件:

  git diff <commit_Id_1> <commit_Id_2> --name-only

注意: 如果您想在提交之间检查差异,您不需要放置提交ID。


8
如果你只想查看最新提交的更改,只需使用git show即可。

8
以下代码将显示当前提交。
git show HEAD

6

更极简的文件变更检查方法(示例)

# 1. Checkout a branch and see the list of commits
git log --oneline -5

# > Output
9b9b1f8 (HEAD -> master) Updated ABC
d58e5da chore: Added files
5a4aa2c chore: Added my pipeline
bb2b0b7 feat: Added ABC
473f711 feat: Added ZYX

# 2. Pick a commit hash and check which files were modified
git show --stat --oneline d58e5da

# > Output
d58e5da chore: Added versioning files
 Someabcfile                            | 18 ++++++++++++++++++
 myfolder/file.py                       | 19 +++++++++++++++++++
 myfolder/file.js                       |  7 +++++++
 myfolder/file.txt                      |  1 +
 4 files changed, 45 insertions(+)

# 3. Pick a file to check the differences
git show d58e5da myfolder12/file.py

或者,您可以从列表中检查单个提交的所有文件差异:

git show d58e5da

5
有些答案忽略了一个特殊情况:如何查看由根提交所做的更改,因为它没有父亲/祖先。
以下两种方法都无法正常使用: git diff <root_commit>^..<root_commit> git diff <root_commit>~..<root_commit>
$git diff 27e521ca73a46b2d3a28568dc49fced81e46aaea~ 27e521ca73a46b2d3a28568dc49fced81e46aaea
fatal: ambiguous argument '27e521ca73a46b2d3a28568dc49fced81e46aaea~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

git diff <root_commit>^!

展示了 根提交 和 HEAD 之间的差异。类似如下:

$ git diff 27e521ca73a46b2d3a28568dc49fced81e46aaea^!
diff --git a/file1.txt b/file1.txt
new file mode 100644
index 0000000..80f3f1a
--- /dev/null
+++ b/file1.txt
@@ -0,0 +1,5 @@
+Create the first file.
+
+Add some placeholder text to first file.
+
+
diff --git a/file2.txt b/file2.txt
new file mode 100644
index 0000000..66e494f
--- /dev/null
+++ b/file2.txt
@@ -0,0 +1,6 @@
+This is the second file.
+
+It has an uncommited commit.
+
+We use it to demo default `git diff` behaviour.
+

(这些是从我的根提交到HEAD所做的所有更改)。

对于根提交,我只发现以下命令有效:

git show --color --pretty=format:%b <root_commit_hash>

用法如下:

$ git show --color --pretty=format:%b 27e521ca73a46b2d3a28568dc49fced81e46aaea

diff --git a/README b/README
new file mode 100644
index 0000000..12a04f0
--- /dev/null
+++ b/README
@@ -0,0 +1,6 @@
+# git-diff-demo
+
+This repo documents the demo of the git diff command.
+We will have options, and use cases.

我的根提交仅添加了README文件。


嗨,什么是“根提交”? - Chan Kim

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