Cyker
Cyker

Reputation: 10914

How to check whether one branch is an ancestor of the other in Git

I have to Git branches A and B. How to check whether A is an ancestor of B, or vice versa?

git merge-base does give the common ancestor. However I'd like to know whether there are even better solutions.

Upvotes: 2

Views: 161

Answers (1)

Andrew C
Andrew C

Reputation: 14853

git merge-base --is-ancestor A B
if [ $? -eq 0 ]
then
   # it's an ancestor
else
   # it's not an ancestor
fi 

This is obviously working on the commits that the branches point to. Git doesn't really track branch lineage the way something like Clearcase does though, so it's quite possible that you could have had A first, then branched off B, and then as a result of some merging end up with B as an ancestor to A.

Upvotes: 4

Related Questions