How to get parent of specific commit in Git?

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

    For the full details: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1

    For just the hash: git log 4c7036e807fa18a3e21a5182983c7c0f05c5936e^ -1 --pretty=%H

    0 讨论(0)
  • 2021-02-02 06:34

    To get Parent Commit

    git cat-file -p commit_id
    
    tree tree_id
    parent parent_commit_id
    [parent other_parent_commit_id] # present only in case of merge commits
    author xxx <xxx@email.com> 1513768542 +0530
    committer xxx <xxx@email.com> 1513768542 +0530 
    
    0 讨论(0)
  • 2021-02-02 06:37
    git log --pretty=%P -n 1 "$commit_from"
    
    0 讨论(0)
  • 2021-02-02 06:40

    If trying to get all parents and using revision parameter syntax, you may try using log subcommand with --no-walk option.

    An example, if we have the following:

    $ git --oneline --graph
    *   A
    |\
    | * B
    | * C
    | * D
    * |   E
    

    In this example, I'll use ^@ to get all parents and the --no-walk option to show only the parents and not their ancestors.

    $ git log --no-walk A^@
    commit B
        ........
    
    commit E
        ........
    

    Check out git rev-parse for more detail about revision parameter.

    0 讨论(0)
  • 2021-02-02 06:40

    If you only want the ids of the parents of an input commit with id <SHA> then run this command:

    git cat-file -p <SHA> | awk 'NR > 1 {if(/^parent/){print $2; next}{exit}}'
    

    This will work for normal and shallow clones.

    0 讨论(0)
  • 2021-02-02 06:43

    To get the parent, just add a ^ after the commit SHA of the commit you want to see.

    To see a commit: git show <SHA> Example: git show bb05425c504575145d005c0a887e0a80b885ced0

    To see the parent: git show <SHA>^
    Example: git show bb05425c504575145d005c0a887e0a80b885ced0^

    0 讨论(0)
提交回复
热议问题