How to understand git log --graph

David Liu picture David Liu · Nov 25, 2013 · Viewed 9.9k times · Source

I am quite confused with the output of graphic git log.

I do understand that each * means a commit, whether it is a diverge, common or merge commit. I do understand that pipes means branch.

Let's look at a simple graph log:

enter image description here

First of all, which branch does the red pipe (the most left-handed one) represent for? I don't think it is the current branch that I am on, because after I checkout to other branch, the graph looks the same. Furthermore, it doesn't represent master branch neither.

Second of all, if the most left-handed branch represents a single branch, why it changes color after commit "0e5b5"?

I searched for a tutorial on how to read git log graphs, unfortunately, I got nothing. If there are some awesome tutorials on this topic, please feel free to share.

Answer

Sebastien picture Sebastien · Nov 25, 2013

Git works from the current commit looking at ancestors. Branches are not "entities", they are (moving) references. There is no way for git log (or gitk which has a different color scheme but is analoguous to git log --graph or tig) to know if the current branch is the descendant of branch A or branch B. It only knows parents. From man git-log:

   git log -p -m --first-parent
       Shows the history including change diffs, but only from the "main 
       branch"   perspective, skipping commits that come from merged
       branches, and showing full diffs of changes introduced by the merges. 
       This makes sense only when following a strict policy of merging
       all topic branches when staying on a single integration branch.

Would somewhat address your concern. git log by default use current checked out commit as a reference (identical to executing git log HEAD

While I personnaly think the man page is quite clear for git, you may want to take a look at gitk or tig. The former is a graphical interface the later is a terminal-like minimal gitk tool. I use both depending on what I want to do.