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

前端 未结 5 860
傲寒
傲寒 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:11

    To build on @helmbert's excellent git related alias, here's a version which also accepts branch names (or HEAD etc) as arguments, rather than just commit ids:

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

提交回复
热议问题