How to display git branches as a tree in CLI

An interesting feature of graphical tools for git is showing a repository tree with all the branches. In this article, I will show how to achieve something similar in the command line.

Example

With the alias I'm using, I can run git tree and see something like:

* 8dc4b3c (HEAD -> main, origin/main, origin/HEAD) add esbuild change
* 4276744 add esbuild logo
* 2dcfa82 import html in esbuild
| * 3d05f80 (solution) script
| * d11dcad update script to cover all 3 gettign started sections
|/  
* 817f5bb starting poing
| * de5c7c6 (final-state) move to mode non
|/  
* 800213b (origin/final-state) add contact-list component
* 97c8010 whole header

It's pretty much everything I need to have an overview of the project, for example:

  • After doing git fetch --all --prune, I can see what is happening on the remote
  • I can see where is my branch in relation to main or other branches
  • In the middle of rebases, I can see how far I already got
  • I can find all commits - especially useful if I search for one I want to cherry-pick

Command

The command that is used, it's git log with some additional flags:

$ git log --oneline --graph --decorate --all

You could type it every time you need it, but for me, it's an everyday command & I hardly ever want to run it differently - it's always the same one, so it makes sense to define an alias for it.

Alias

For defining alias, you can use your~/.gitconfig. If the file is missing, you can create it. It's the same file where git saves your name & email, so most likely, you have at least something like:

[user]
        email = your@email
        name = Your Name

after those lines, you can add:

[alias]
        tree = log --oneline --graph --decorate --all

If you already have some aliases, you only need to add the tree = ... to the alias section.

Then, you will have git tree available.

Summary

We have seen how to set up a git tree alias to improve your git CLI experience.

29