Last Updated: December 31, 2020
·
1.47K
· bsimpson

Show matching git ref names

Have you ever fired up a git GUI and seen this:

Picture

These labels comprise a list of other refs that share the same HEAD as your current HEAD. If you have ever wanted to calculate this in the CLI, you can run the following command:

git log --format="%d" -n 1 <ref name>

What does this do?

  • git-log has a formatting command that allows you to build a customized output.
  • This is invoked via the --format or --pretty argument. The %d placeholder prints ref names.
  • Since we are only concerned about the most recent commit (HEAD), we can limit the log output to only display the latest commit via the -n 1 argument.
  • Optionally, we can specify any valid <ref name>. If this is omitted, it defaults to HEAD.

When is this useful?

Using this method, you can calculate what parent branch your branch comes from. For example, if you create a branch, and introduce 1 commit, you can issue the command: git log --format="%d" -n 1 HEAD~1 to discover the parent's branch name.

This also works with git tags. If you want to see what tags are applied to a branch, you can issue this command to list them all.

Another use is checking whether a remote branch is up-to-date with your local branch, or if you have commits you haven't pushed yet.

Extra tip: The ref names can also be displayed with the shorter, but more cluttered git log --decorate argument.