简体   繁体   English

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

[英]How can I see the changes in a Git commit?

When I do git diff COMMIT I see the changes between that commit and HEAD (as far as I know), but I would like to see the changes that were made by that single commit.当我执行git diff COMMIT时,我看到了该提交和 HEAD 之间的更改(据我所知),但我想查看该单个提交所做的更改。

I haven't found any obvious options on diff / log that will give me that output.我没有在diff / log上找到任何明显的选项可以给我 output。

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit:要查看特定COMMIT哈希的差异,其中COMMIT是提交的哈希:

git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT . git diff COMMIT~ COMMIT会告诉你那个COMMIT的祖先和COMMIT之间的区别。 See the man pages for git diff for details about the command and gitrevisions about the ~ notation and its friends.有关命令的详细信息和有关~符号及其朋友的gitrevisions,请参阅git diff的手册页。

Alternatively, git show COMMIT will do something very similar.或者, git show COMMIT会做一些非常相似的事情。 (The commit's data, including its diff - but not for merge commits.) See the git show manpage . (提交的数据,包括其差异 - 但不适用于合并提交。)请参阅git show 手册页

(also git diff COMMIT will show you the difference between that COMMIT and the head.) (也git diff COMMIT将向您展示该COMMIT和头部之间的区别。)

As mentioned in " Shorthand for diff of git commit with its parent? ", you can also use git diff with:如“ git commit 与其父项的差异的简写? ”中所述,您还可以将git diff用于:

git diff COMMIT^!

or要么

git diff-tree -p COMMIT

With git show, you would need (in order to focus on diff alone) to do:使用 git show,您需要(为了只关注 diff)执行以下操作:

git show --color --pretty=format:%b COMMIT

The COMMIT parameter is a commit-ish : COMMIT参数是一个commit-ish

A commit object or an object that can be recursively dereferenced to a commit object.commit对象或一个对象可以被递归地解除引用到一个提交对象。 The following are all commit-ishes: a commit object, a tag object that points to a commit object, a tag object that points to a tag object that points to a commit object, etc.以下是所有提交-ishes:提交对象,标签对象是指向一个commit对象,标签对象指向一个标签对象指向一个commit对象,等等。

See gitrevision "SPECIFYING REVISIONS" to reference a commit-ish.请参阅gitrevision "SPECIFYING REVISIONS"以引用 commit-ish。
See also " What does tree-ish mean in Git? ".另请参阅“ Git 中 tree-ish 是什么意思? ”。

你也可以试试这个简单的方法:

git show <COMMIT>

git show shows the changes made in the most recent commit. git show显示在最近一次提交中所做的更改。 It is equivalent to git show HEAD .它相当于git show HEAD

git show HEAD~1 takes you back one commit. git show HEAD~1带你返回一次提交。

I usually do:我通常这样做:

git diff HEAD~1

To show the changes regarding the last commit.显示有关上次提交的更改。 If you have more commits just increase the number 1 to how many commits diff you want to see.如果您有更多提交,只需将数字 1 增加到您想要查看的提交差异数量。

Use:采用:

git show <commit_sha>

This will show you just what's in that commit.这将向您展示该提交中的内容。 You can do a range by just putting a space between the two commit SHA-1 hashes.您可以通过在两个提交 SHA-1 哈希之间放置一个空格来确定范围。

git show <beginning_sha> <ending_sha>

which is pretty helpful if you're rebasing often because your feature logs will all be in a row.如果您经常进行变基,这非常有用,因为您的功能日志将全部排成一行。

If you happen to want to look at the last 3 commits you can use the HEAD syntax如果您想查看最后 3 次提交,您可以使用 HEAD 语法

git show HEAD~3 HEAD

First get the commit ID using,首先使用提交 ID,

git log #to list all

Or要么

git log -p -1 #last one commit id

Copy commit id.复制提交 ID。

Now we use two methods to list changes from a specific commit,现在我们使用两种方法来列出来自特定提交的更改,

Method 1:方法一:

git diff commit_id^! #commit id something like this 1c6a6000asad012

Method 2:方法二:

git show commit_id
For example: git show 1c6a600a

From the man page for git-diff(1) :git-diff(1)的手册页:

git diff [options] [<commit>] [--] [<path>…]
git diff [options] --cached [<commit>] [--] [<path>…]
git diff [options] <commit> <commit> [--] [<path>…]
git diff [options] <blob> <blob>
git diff [options] [--no-index] [--] <path> <path>

Use the 3rd one in the middle:使用中间的第三个:

git diff [options] <parent-commit> <commit>

Also from the same man page, at the bottom, in the Examples section :同样来自同一手册页的底部,在示例部分

$ git diff HEAD^ HEAD      <3>

Compare the version before the last commit and the last commit.比较上次提交和上次提交之前的版本。

Admittedly it's worded a little confusingly, it would be less confusing as诚然,它的措辞有点令人困惑,因为它不会那么令人困惑

Compare the most recent commit with the commit before it.将最近的提交与其之前的提交进行比较。

The following seems to do the job;以下似乎可以完成这项工作; I use it to show what has been brought in by a merge.我用它来显示合并带来了什么。

git whatchanged -m -n 1 -p <SHA-1 hash of merge commit>

另一种可能:

git log -p COMMIT -1

I like the below command to compare a specific commit and its last commit:我喜欢下面的命令来比较特定的提交和它的最后一次提交:

git diff <commit-hash>^-

Example:例子:

git diff cd1b3f485^-

You could use git diff HEAD HEAD^1 to see the diff with the parent commit.您可以使用git diff HEAD HEAD^1来查看与父提交的差异。

If you only want to see the list of files, add the --stat option.如果您只想查看文件列表,请添加--stat选项。

git difftool COMMIT^ <commit hash>

is also possible if you have configured your difftool.如果您配置了 difftool,也可以使用。

See here how to configure difftool .请参阅此处如何配置 difftool Or the manual page here .或者这里的手册页。

Additionally, you can use git diff-tree --no-commit-id --name-only -r <commit hash> to see which files been changed/committed in a give commit hash.此外,您可以使用git diff-tree --no-commit-id --name-only -r <commit hash>查看在给定提交哈希中更改/提交了哪些文件。

To see author and time by commit, use git show COMMIT .要通过提交查看作者和时间,请使用git show COMMIT Which will result in something like this:这将导致这样的事情:

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'

If you want to see which files had been changed, run the following with the values from the Merge line above, git diff --stat a2a2894 3a1ba8f .如果您想查看哪些文件已更改,请使用上面Merge行中的值运行以下命令, git diff --stat a2a2894 3a1ba8f

If you want to see the actual diff, run git --stat a2a2894 3a1ba8f .如果您想查看实际差异,请运行git --stat a2a2894 3a1ba8f

如果您只想查看最新提交中的更改,只需git show即可。

For checking complete changes:要检查完整的更改:

  git diff <commit_Id_1> <commit_Id_2>

For checking only the changed/added/deleted files:仅检查更改/添加/删除的文件:

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

NOTE : For checking diff without commit in between, you don't need to put the commit ids.注意:为了检查差异而不在两者之间提交,您不需要放置提交 ID。

以下代码将显示当前提交

git show HEAD

This command will get you the Git parent commit-hash:此命令将为您提供 Git 父提交哈希:

git log -n 2 <commit-hash>

After that git diff-tool <commit-hash> <parent-commit-hash>之后git diff-tool <commit-hash> <parent-commit-hash>

Example:例子:

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.

After this在这之后

git difftool 7f65b9a9d3820525766fcba285b3c678e889fe3b c3a61f17e14e2b80cf64b172a45f1b4826ee291f

I'm running Git version 2.6.1.windows.1 on Windows 10, so I needed a slight modification to Nevik's answer (tilde instead of caret):我在 Windows 10 上运行 Git 版本 2.6.1.windows.1,所以我需要对 Nevik 的答案稍作修改(波浪号而不是插入符号):

git diff COMMIT~ COMMIT

Another option is to quote the caret:另一种选择是引用插入符号:

git diff "COMMIT^" COMMIT

还可以查看特定文件的两次提交之间的更改。

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

A few answers miss a special case.一些答案错过了一个特殊情况。 How to view changes made by the Root Commit as it does not have a parent/ancestor.如何查看根提交所做的更改,因为它没有父/祖先。

Both两个都

git diff <root_commit>^..<root_commit>

and

git diff <root_commit>~..<root_commit>

throw an error.抛出错误。

$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>^!

shows diff btw root commit and HEAD.显示 diff btw root commit和 HEAD。 Like so:像这样:

$ 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.
+

(These are changes made by all commits btw my root commit and HEAD). (这些是我的根提交和 HEAD 的所有提交所做的更改)。

For Root Commit对于根提交

I find only我发现只有

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

works.作品。

Like so:像这样:

$ 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.

(My root commit added only the README) (我的根提交只添加了自述文件)

More minimalist approach for checking file changes (example)检查文件更改的更简约方法(示例)

# 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

Or, alternatively, check all file differences within a single commit from the list:或者,或者,检查列表中单个提交中的所有文件差异:

git show d58e5da

In case of checking the source change in a graphical view , use:如果在图形视图中检查源更改,请使用:

gitk (your commit id goes here)

For example:例如:

gitk HEAD~1 

For me this works just fine对我来说这很好用

git show COMMIT --compact-summary

Which shows the next information其中显示了下一个信息

Output a condensed summary of extended header information such as file creations or deletions ("new" or "gone", optionally "+l" if it's a symlink) and mode changes ("+x" or "-x" for adding or removing executable bit respectively) in diffstat.输出扩展头信息的精简摘要,例如文件创建或删除(“new”或“gone”,如果是符号链接,则可选“+l”)和模式更改(“+x”或“-x”用于添加或删除diffstat 中的可执行位)。 The information is put between the filename part and the graph part.信息放在文件名部分和图形部分之间。 Implies --stat.暗示--stat。

git difftool "HASH_in_history" -- "path_to_file" git difftool "HASH_in_history" -- "path_to_file"

  1. You can click on each commit on the specific address of git to view可以在git的具体地址上点击各个commit查看
  2. If you submit with a tool, you can pass show history如果您使用工具提交,则可以传递显示历史记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM