Git: show commits in a branch since the branching point

If you are in a git branch and want to see all commits since the branch point you can use this one-liner.

git diff $(git merge-base master HEAD)

For example, with the following topology, the merge base between master and branch is 1.

         o---o---o---branch
        /
---o---1---o---o---o---master

There are a lot of hidden details in this compact command, so let’s analyze it in detail. The long form is:

git diff $(git merge-base $(git rev-parse master) $(git rev-parse HEAD))..HEAD

Let’s look at this from the inside out:

  • git rev-parse master prints the SHA-1 of the latest commit in the master branch.
  • git rev-parse HEAD prints the SHA-1 of the latest commit in the current branch.
  • git merge-base <commit1> <commit2> finds the common ancestor of the two commits.
  • git diff <commit1>..<commit2> shows the changes between the two commits.

There are various ways to make this more compact. In git merge-base, you can just use master to mean git rev-parse master, and HEAD to mean git rev-parse HEAD.

And for git diff <commit1>..<commit2>, the default for <commit2> is HEAD. If only <commit1> is given, ..HEAD is assumed.

Pin It on Pinterest