1

I want to be able to understand between which 2 tags (tags which represent versions of my app) a commit is.

Is there a command for discerning if Commit A is a parent higher up the tree of commit B.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
JY2k
  • 2,879
  • 1
  • 31
  • 60

3 Answers3

1

Regarding tags

You can at least know if your commit is done after a tag with git describe:

 git describe --tags
 aTagName-n-gxxxx

aTagName represents the closest tag from your current commit.
If your commit is past that tag, you won't get just its name, but aTagName-n-gxxxx, with n being the number of commits after that tag to your current commit, represented by the SHA1 xxx.

You can combine that with git tag --contains <yourCommit> in order to get the tags which includes your commit: the last one should be the oldest one whose history still include your commit.

Regarding commits:

Is there a command for discerning if Commit A is a parent higher up the tree of commit B.

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

git merge-base --is-ancestor <commit1> <commit2>
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

The simplest way to show it quickly is to run

$ git log --decorate --oneline A..B

and just see which tag appears first in the history.

(oneline is just to reduce paging in case there are lots of commits, and decorate is to show the tags).


Note (see Specifying Ranges on the form r1..r2) that this runs merge-base internally, so if you're doing this a lot, running merge-base directly probably is better ... but you need to examine the return value. You could just write a shell function, or script, and add a git alias to do it nicely:

function ancestor()
{
    if git merge-base --is-ancestor $1 $2; then
        echo "$1 is ancestor of $2";
    else
        if git merge-base --is-ancestor $2 $1; then
            echo "$2 is ancestor of $1";
        else
            echo "no parent-child relationship at all";
        fi;
    fi
}

This will also tell you if neither commit is an ancestor of the other. If you want to know which is above the other in that case, first decide what that means, and then choose one of the commit ordering options to git-log.

Useless
  • 64,155
  • 6
  • 88
  • 132
0

Assuming A and B are not referring to the same commit, git merge-base A B

If the result is A's commit, then A is an ancestor of B.

If the result is B's commit, then B is an ancestor of A.

If the result is neither A nor B, then A and B are diverged. But they have at lease one common ancestor.

If the result is empty, then A and B are from two branches and they have no common ancestor.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53