How to get parent of specific commit in Git?

前端 未结 8 1235
别跟我提以往
别跟我提以往 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:54

    If ${SHA} is the commit you know and you want its parent (assuming it's not a merge commit and has only one parent):

    git rev-parse ${SHA}^
    
    0 讨论(0)
  • 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:

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