How can I tell if one commit is an ancestor of another commit (or vice-versa)?

前端 未结 5 862
傲寒
傲寒 2020-12-02 16:24

Git is a DAG of snapshots, with each node on the graph representing a commit. Each commit can have \'n\' parent commits.

Given any two commits, is there a single, co

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 17:12

    The following shell script might do the trick:

    if git rev-list $SHA1 | grep -q $SHA2 ; then echo "$SHA2 is ancestor of $SHA1"
    elif git rev-list $SHA2 | grep -q $SHA1 ; then echo "$SHA1 is ancestor of $SHA2"
    else echo "$SHA1 unrelated to $SHA2" ; fi
    

    Or, to neatly wrap it up into a git alias:

    git config --global alias.related '!function git_related() { if git rev-list $1 | grep -q $2 ; then echo "$2 is ancestor of $1" ; elif git rev-list $2 | grep -q $1 ; then echo "$1 is ancestor of $2" ; else echo "$1 unrelated to $2" ; fi } ; git_related $1 $2'
    

提交回复
热议问题