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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17563726/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:31:08  来源:igfitidea点击:

How to see the changes in a Git commit?

gitversion-controldiffgit-diff

提问by laktak

When I do git diff COMMITI 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/ logthat will give me that output.

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

回答by Nevik Rehnel

To see the diff for a particular COMMIThash:

要查看特定COMMIT哈希的差异 :

git diff COMMIT~ COMMITwill show you the difference between that COMMIT's ancestor and the COMMIT. See the man pages for git difffor details about the command and gitrevisionsabout the ~notation and its friends.

git diff COMMIT~ COMMIT将向您展示 thatCOMMIT的祖先和COMMIT. 请参阅git diff的手册页以获取有关该命令的详细信息以及有关该符号及其朋友的gitrevisions~

Alternatively, git show COMMITwill do something very similar. (The commit's data, including its diff - but not for merge commits.) See the git show manpage.

或者,git show COMMIT会做一些非常相似的事情。(提交的数据,包括其差异 - 但不适用于合并提交。)请参阅git show 手册页

回答by VonC

As mentioned in "Shorthand for diff of git commit with its parent?", you can also use git diffwith:

如“ 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 COMMITparameter is a commit-ish:

COMMIT参数是一个commit-ish

A commit objector an objectthat can be recursively dereferenced to a commit object. The following are all commit-ishes: a commit object, a tag objectthat points to a commit object, a tag object that points to a tag object that points to a commit object, etc.

commit对象或一个对象可以被递归地解除引用到一个提交对象。以下是所有提交-ishes:提交对象,标签对象是指向一个commit对象,标签对象指向一个标签对象指向一个commit对象,等等。

See gitrevision "SPECIFYING REVISIONS"to reference a commit-ish.
See also "What does tree-ish mean in Git?".

请参阅gitrevision "SPECIFYING REVISIONS"以引用 commit-ish。
另请参阅“ Git 中的 tree-ish 是什么意思?”。

回答by Lakhan

You can also try this easy way:

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

git show <COMMIT>

回答by Adam Salma

git showshows the changes made in the most recent commit.

git show显示在最近一次提交中所做的更改。

Equivalent to git show HEAD.

相当于git show HEAD

git show HEAD~1takes you back 1 commit.

git show HEAD~1带你回 1 次提交。

回答by Alex

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 增加到您想要查看的提交差异数量。

回答by Mohideen bin Mohammed

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

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

Method 2:

方法二:

git show commit_id
For example: git show 1c6a600a

回答by Iwnnay

git show <commit_sha>

This will show you just what's in that commit. I think you can do a range it by just putting a space between two commit shas.

这将向您展示该提交中的内容。我认为您可以通过在两个提交 shas 之间放置一个空格来实现它的范围。

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.

如果您经常进行变基,这非常有用,因为您的功能日志将全部排成一行。

回答by Iwnnay

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.

将最近的提交与其之前的提交进行比较。

回答by MichaelMoser

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>

回答by John_West

Another possibility:

另一种可能:

git log -p COMMIT -1