简体   繁体   中英

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

"git cat-file -p HEAD^2" gives me the following error:

fatal: Not a valid object name HEAD^2

However, using the "~" format, it works:

$ git cat-file -p HEAD~2

gitrevision man page says the following:

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

What have I misunderstood?

You have a similar error message on Windows (in a CMD session, not in a git-bash session).
You need to escape the ^ , with an ^ .

git cat-file -p HEAD^^2

But that won't work unless HEAD has two parents (meaning is the result of a merge), which seems to be the case here.

For instance:

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>

That gives:

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

Since your error message is Not a valid object name HEAD^2 , instead of Not a valid object name @2 , that means the HEAD has only one parent.


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

No:

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

@~2 is X , while @^2 is Y .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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