如何区分提交与其父提交

发布于 2024-07-10 20:58:48 字数 153 浏览 8 评论 0原文

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

git diff 15dc8^..15dc8

如果您只提供单个提交 ID git diff 15dc8,它会根据 HEAD 进行比较。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

傲娇萝莉攻 2024-07-17 20:58:48

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

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

音盲 2024-07-17 20:58:48

使用:

git diff 15dc8^!

git- 的以下片段中所述rev-parse(1) 手册页(或在现代 Git gitrevisions(7) 手册页):

另外两个用于命名由提交及其组成的集合的简写
父提交存在。 r1^@ 表示法表示 r1 的所有父代。 r1^!
包括提交 r1 但不包括其所有父项。

这意味着您可以在 Git 中任何需要修订的地方使用 15dc8^! 作为 15dc8^..15dc8 的简写。 对于 diff 命令,git diff 15dc8^..15dc8 被理解为 git diff 15dc8^ 15dc8,这意味着与父级之间的差异提交(15dc8^)并提交(15dc8)。

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


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

Use:

git diff 15dc8^!

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

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.

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

Note: the description in git-rev-parse(1) man page 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 ..."


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

内心荒芜 2024-07-17 20:58:48

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

# Current branch vs. parent
git diff HEAD^ HEAD

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

先前提交的工作方式如下:

# Parent of HEAD
git show HEAD^1

# Grandparent
git show HEAD^2

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

# Great grandparent
git show HEAD~3

请参阅 此页面了解详细信息

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.

可可 2024-07-17 20:58:48

正如 mpadi 指出的,您可以使用git show $COMMIT,但这也显示了一些标头和提交消息。 如果您想要直接比较,请使用 git show --pretty=format:%b $COMMIT。

这显然不是一个非常简单的方法,所以我将这个别名保留在我的 .gitconfig 中,

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

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

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.

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

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

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

画尸师 2024-07-17 20:58:48

如果您使用 Z shell 并设置了 extendedglob 选项。 您可以通过以下三种方法之一修复它:

  1. unsetoptextendglob(和/或从 .zshrc 中将其删除)

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

  3. 每次都用反斜杠转义脱字符号和 bang,例如 git diff 15dc8\^\!< /p>

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

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

堇年纸鸢 2024-07-17 20:58:48

保罗的解决方案做了我所希望的会。

$ git diff HEAD^1

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

[alias]
    diff-last = diff HEAD^1

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

$ git diff HEAD^1 HEAD

Paul's solution 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 the shorthand to view diff between head and previous.

[alias]
    diff-last = diff HEAD^1

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

$ git diff HEAD^1 HEAD
々眼睛长脚气 2024-07-17 20:58:48
git diff 15dc8 15dce~1

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

git diff 15dc8 15dce~1

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

梅窗月明清似水 2024-07-17 20:58:48

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

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"

This uses aliases, so it 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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文