How to get the parents of a merge commit in git?

前端 未结 2 1881
[愿得一人]
[愿得一人] 2020-11-30 19:46

Some git commands take the parent as a revision; others (such as git revert), as a parent number. How to get the parents for both cases. I don’t want to use the

2条回答
  •  暖寄归人
    2020-11-30 20:06

    Simple git log called for a merge commit shows abbreviated hashes of its parents:

     $ git log -1 395f65d
     commit 395f65d438b13fb1fded88a330dc06c3b0951046
     Merge: 9901923 d28790d
     ...
    

    git outputs parents according to their number: the first (leftmost) hash is for the first parent, and so on.

    If all you want is just the hashes, the two equivalent choices are:

    $ git log --pretty=%P -n 1 
    $ git show -s --pretty=%P 
    

    git rev-list can also show the parents' hashes, though it will first list the hash for a commit:

    $ git rev-list --parents -n 1 
    

    If you want to examine the parents, you can refer to them directly with carats as ^1 and ^2, e.g.:

    git show ^1
    

    This does generalize; for an octopus merge you can refer to the nth parent as ^n. You can refer to all parents with ^@, though this doesn't work when a single commit is required. Additional suffixes can appear after the nth parent syntax (e.g. ^2^, ^2^@), whereas they cannot after ^@ (^@^ isn't valid). For more on this syntax, read the rev-parse man page.

提交回复
热议问题