How to get parent of specific commit in Git?

前端 未结 8 1220
别跟我提以往
别跟我提以往 2021-02-02 06:25

I have commit number. I would like to get previous commit number(parent). I need commits from current branch.

8条回答
  •  不思量自难忘°
    2021-02-02 06:57

    You can use git rev-parse for this.

    # Output hash of the first parent
    git rev-parse $commit^
    
    # Nth parent
    git rev-parse $commit^N
    
    # All parents
    git rev-parse $commit^@
    

    These constructs are explained in git help rev-parse:

     ^, e.g. HEAD^, v1.5.1^0
         A suffix ^ to a revision parameter means the first
         parent of that commit object.  ^ means the th
         parent (i.e.  ^ is equivalent to ^1). As a
         special rule, ^0 means the commit itself and is
         used when  is the object name of a tag object that
         refers to a commit object.
    
     ...
    
     ^@, e.g. HEAD^@
         A suffix ^ followed by an at sign is the same as listing
         all parents of  (meaning, include anything
         reachable from its parents, but not the commit itself).
    

提交回复
热议问题