为什么 "git cat-file -p HEAD^2" 会失败?

Why does "git cat-file -p HEAD^2" fail?

"git cat-file -p HEAD^2" 给我以下错误:

fatal: Not a valid object name HEAD^2

但是,使用“~”格式,它有效:

$ git cat-file -p HEAD~2

gitrevision man page 表示如下:

... A suffix ^ to a revision parameter means the first parent of that commit object. rev^n means the nth parent (i.e. rev^ is equivalent to rev^1)

我误会了什么?

您在 Windows 上有类似的错误消息(在 CMD 会话中,而不是在 git-bash 会话中)。
您需要使用 ^.

转义 ^
git cat-file -p HEAD^^2

但这不会起作用,除非 HEAD 有两个父项(意思是合并的结果),这里似乎就是这种情况。

例如:

C:\Users\vonc\prog\git\git>gl
*   74a844a - (HEAD -> master) Merge branch 'rj/mailmap-ramsay' (6 days ago) <Junio C Hamano>
|\
| * dafc047 - mailmap: update my entry with new email address (11 days ago) <Ramsay Jones>
* |   b6bd2d0 - Merge branch 'bn/send-email-smtp-auth-error-message-fix' (6 days ago) <Junio C Hamano>

这给出:

C:\Users\vonc\prog\git\git>git cat-file -p @^2
fatal: Not a valid object name @2

C:\Users\vonc\prog\git\git>git cat-file -p @^^2
tree c78343c7a98b4cb8a455d73aeecaa8acfa2cb30e
parent f4d9753a89bf04011c00e943d85211906e86a0f6
author Ramsay Jones <ramsay@ramsayjones.plus.com> 1441122606 +0100
committer Junio C Hamano <gitster@pobox.com> 1442419728 -0700

由于您的错误消息是 Not a valid object name HEAD^2,而不是 Not a valid object name @2,这意味着 HEAD 只有一个父级。


HEAD~2" works for me. Isn't "HEAD^n" equivalent to "HEAD~n"?

否:

HEAD~ and HEAD^ are equivalent.
The difference becomes apparent when you specify a number.
HEAD~2 means “the first parent of the first parent,” or “the grandparent”

X--x--x
     /
    Y

@~2X,而 @^2Y.