git 如何区分提交与其父提交?

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

How to diff a commit with its parent?

gitgit-diff

提问by Brian L

Aside from writing an alias or script, is there a shorter command for getting the diff for a particular commit?

除了编写别名或脚本之外,是否有更短的命令来获取特定提交的差异?

git diff 15dc8^..15dc8

If you only give the single commit id git diff 15dc8, it diffs that commit against HEAD.

如果您只提供单个提交 id git diff 15dc8,则该提交与 HEAD 不同。

回答by mipadi

Use git show $COMMIT. It'll show you the log message for the commit, and the diff of that particular commit.

使用git show $COMMIT. 它将向您显示提交的日志消息,以及该特定提交的差异。

回答by Jakub Nar?bski

Use:

用:

git diff 15dc8^!

as described in the following fragment of git-rev-parse(1)manpage (or in modern git gitrevisions(7)manpage):

如以下git-rev-parse(1)联机帮助页片段(或现代 git gitrevisions(7)联机帮助页)中所述:

Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The r1^@ notation means all parents of r1. r1^! includes commit r1 but excludes all of its parents.

存在用于命名由提交及其父提交形成的集合的另外两个简写。r1^@ 符号表示 r1 的所有父节点。r1^!包括提交 r1 但排除其所有父项。

This means that you can use 15dc8^!as a shorthand for 15dc8^..15dc8anywhere in git where revisions are needed. For diffcommand the git diff 15dc8^..15dc8is understood as git diff 15dc8^ 15dc8, which means the difference between parent of commit (15dc8^) and commit (15dc8).

这意味着您可以在 git 中需要修改的任何地方使用15dc8^!速记15dc8^..15dc8。对于diff命令, thegit diff 15dc8^..15dc8被理解为git diff 15dc8^ 15dc8,这意味着 commit ( 15dc8^) 和 commit ( 15dc8) 的父级之间的差异。

Note: the description in git-rev-parse(1)manpage talks about revision ranges, where it needs to work also for merge commits, with more than one parent. Then r1^!is "r1 --not r1^@" i.e. "r1 ^r1^1 ^r1^2 ..."

注意:手册git-rev-parse(1)页中的描述讨论了修订范围,它也需要用于合并提交,并且具有多个父级。然后r1^!是“ r1 --not r1^@”即“ r1 ^r1^1 ^r1^2 ...



Also, you can use git show COMMITto get commit description and diff for a commit. If you want only diff, you can use git diff-tree -p COMMIT

此外,您可以使用git show COMMIT获取提交描述和提交的差异。如果你只想要差异,你可以使用git diff-tree -p COMMIT

回答by Paul Vincent Craven

If you know how far back, you can try something like:

如果您知道多远,您可以尝试以下操作:

# Current branch vs. parent
git diff HEAD^ HEAD

# Current branch, diff between commits 2 and 3 times back
git diff HEAD~3 HEAD~2

Prior commits work something like this:

先前的提交工作是这样的:

# Parent of HEAD
git show HEAD^1

# Grandparent
git show HEAD^2

There are a lot of ways you can specify commits:

您可以通过多种方式指定提交:

# Great grandparent
git show HEAD~3

See this page for details.

有关详细信息,请参阅此页面

回答by ?ystein Steimler

As @mipadi points out, you can use git show $COMMIT, but this also shows some headers and the commit message. If you want a straight diff, use git show --pretty=format:%b $COMMIT.

正如@mipadi 指出的那样,您可以使用git show $COMMIT,但这也会显示一些标题和提交消息。如果您想要直接差异,请使用git show --pretty=format:%b $COMMIT.

This is, obviously not a very short hand, so I'm keeping this alias in my .gitconfig

这显然不是一个很短的手,所以我在我的 .gitconfig 中保留了这个别名

    [alias]
      sd = show --pretty=format:%b

This enables me to use git sd $COMMITto show diff.

这使我能够git sd $COMMIT用来显示 diff

回答by Ville

Many of the mentioned examples (e.g. git diff 15dc8^!, or git diff 15dc8^..15dc8) don't work if you are using zsh and have extendedgloboption set. You can fix it by one of the following three ways:

如果您使用 zsh 并设置了选项git diff 15dc8^!,则许多提到的示例(例如, 或git diff 15dc8^..15dc8)将不起作用extendedglob。您可以通过以下三种方式之一进行修复:

  1. unsetopt extendedglob(and/or remove it from .zshrc)

  2. setopt NO_NOMATCH(and/or set it in .zshrc)

  3. escape the caret and bang every time with a backslash, e.g. git diff 15dc8\^\!

  1. unsetopt extendedglob(和/或从 .zshrc 中删除它)

  2. setopt NO_NOMATCH(和/或在 .zshrc 中设置)

  3. 每次都用反斜杠逃避插入符号并砰砰,例如 git diff 15dc8\^\!

回答by John Lawrence Aspden

git diff 15dc8 15dce~1

~1 means 'parent', ~2 'grandparent, etc.

~1 表示“父母”,~2 表示“祖父母”等。

回答by Graham R. Armstrong

Paul's solution above did what I was hoping it would.

保罗上面的解决方案达到了我的希望。

$ git diff HEAD^1

Also, it's useful to add aliases like hobs mentioned, if you put the following in the [alias] section of your ~/.gitconfig file then you can use short-hand to view diff between head and previous.

此外,添加像 hobs 提到的别名很有用,如果您将以下内容放在 ~/.gitconfig 文件的 [alias] 部分,那么您可以使用简写来查看 head 和 previous 之间的差异。

[alias]
    diff-last = diff HEAD^1

Then running $ git diff-lastwill get you your result. Note that this will also includeany changes you've not yet committed as well as the diff between commits. If you want to ignorechanges you've not yet committed, then you can use diff to compare the HEAD with it's parent directly:

然后运行$ git diff-last会得到你的结果。请注意,这还将包括您尚未提交的任何更改以及提交之间的差异。如果您想忽略尚未提交的更改,则可以使用 diff 直接将 HEAD 与其父项进行比较:

$ git diff HEAD^1 HEAD

回答by hobs

Uses aliases, so doesn't answer your question exactly but I find these useful for doing what you intend...

使用别名,所以不能准确地回答你的问题,但我发现这些对于做你想做的事情很有用......

alias gitdiff-1="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 2|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git diff"
alias gitdiff-2="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 3|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git diff"
alias gitdiff-3="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 4|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git diff"

alias gitlog-1="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 2|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git log --summary"
alias gitlog-2="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 3|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git log --summary"
alias gitlog-3="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 4|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git log --summary"