How to display Git branches easily as a tree in CLI

How to display Git branches easily as a tree in CLI

Git is a tool that employs a command line interface (CLI). For many beginner programmers, it’s the first one of this kind they learn. This alone could be confusing, but on top of that, Git’s default responses do not clearly visualize the state of your repository.

A helpful feature of graphical tools for Git is showing a repository tree with all the branches. This allows you to see the history of your current repository, where you are right now, and how it relates to other branches. This provides a lot of information in one glimpse.

Luckily, you don't need to install any graphic tools for Git to produce a visualization of all the branches! In this article, I will show you how to achieve something equally helpful in the command line alone.

The result

The objective is to get 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

The above is 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—whether there are any new commits, or whether any of the branches were rebased and force pushed
  • I can see where my branch is in relation to main or other branches—especially, I can see if any of the branches need a rebase on the most recent main
  • In the middle of rebases, I can see how far I’ve already gotten—I can compare what commits are in my in-progress branch (the one that HEAD points to), and the original branch I’m rebasing
  • I can find all commits—I can see what was already merged to main, or I can find commits I want to cherry-pick


The command we're about to use is basically git log with a few additional flags:

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

Each flag has the following meaning:

  • --oneline—This summarizes the commit in one line. Instead of the verbose output shown by git log, we get something like for each commit 8dc4b3c add esbuild change.
  • --graph—Adds stars and lines to commits so you can see how branches relate to one another.
  • --decorate—Shows all branches and tags pointing to each commit. Depending on the configuration of your system, this can happen by default so the flag can be skipped.
  • --all—Shows all commits—not only the ones in the current branch.

You could type it every time you need to, but for me, it's a command I want to run exactly like this all the time. In such a case, it makes sense to define an alias for it.


Aliases are a way you can customize your Git CLI. You can define a new ‘command’ that combines a standard command with some flags or parameters you often use. There are two ways you can define a new alias:

config command

The easiest way is to run the following Git command:

$ git config --global alias.tree 'log --oneline --graph --decorate --all'

The expected result is no output. If you want to verify that the command runs correctly, then you can check whether the value was saved correctly:

$ git config --global alias.tree
log --oneline --graph --decorate --all

and then run the following command:

$ git tree
* 8dc4b3c (HEAD -> main, origin/main, origin/HEAD) add esbuild change
* 4276744 add esbuild logo

Configuration file

Alternatively, you set it up directly in your ~/.gitconfig. It's the same file where Git saves your name and email, so you’ll most likely have at least something like:

        email = your@email
        name = Your Name

After those lines, you can add:

        tree = log --oneline --graph --decorate --all

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

If the file is missing, then create a new file called .gitignore in your home directory and add both user and alias sections.

That’s it! Now, you will have git tree available.

Additional parameters

Our alias is nothing more than a git log and few flags, and we can add additional parameters. The most useful one is -<number>—to limit the commits displayed to the <number> most recent ones. When I work, I use it all the time:

$ git tree -2 
* 8dc4b3c (HEAD -> main, origin/main, origin/HEAD) add esbuild change
* 4276744 add esbuild logo

What next?

Git has many confusing aspects when you start using it, but it gets way simpler once you understand more clearly the way it stores its data. If you are interested in learning more about Git, sign up here to get updates about my Git-focused content.