Update: I've posted an improved version of this answer to the Visualizing branch topology in Git question, since it's far more appropriate there. Leaving this answer for historical (& rep, I'll admit) reasons, though I'm really tempted to just delete it.

My two cents: I have two aliases I normally throw in my ~/.gitconfig file:

[alias]
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
lg = lg1

git lg/git lg1 looks like this:

and git lg2 looks like this:


(Note: There now exists much more applicable answers to this question, such as fracz's, Jubobs', or Harry Lee's!)

Answer from Slipp D. Thompson on Stack Overflow
Top answer
1 of 16
2435

Update: I've posted an improved version of this answer to the Visualizing branch topology in Git question, since it's far more appropriate there. Leaving this answer for historical (& rep, I'll admit) reasons, though I'm really tempted to just delete it.

My two cents: I have two aliases I normally throw in my ~/.gitconfig file:

[alias]
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
lg = lg1

git lg/git lg1 looks like this:

and git lg2 looks like this:


(Note: There now exists much more applicable answers to this question, such as fracz's, Jubobs', or Harry Lee's!)

2 of 16
2189

Many of the answers here are great, but for those that just want a simple one-line-to-the-point answer without having to set up aliases or anything extra, here it is:

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

Not everyone would be doing a git log all the time, but when you need it just remember:

"A Dog" = git log --all --decorate --oneline --graph

If you enter

git config --global alias.adog "log --all --decorate --oneline --graph"

at the command prompt once, you can use

git adog

from that prompt even if you close and reopen it.

🌐
Git
git-scm.com › docs › git-log
Git - git-log Documentation
When using the default mode, N and R both have a TREESAME parent, so those edges are walked and the others are ignored. The resulting history graph is: ... When using --full-history, Git walks every edge. This will discover the commits A and B and the merge M, but also will reveal the merge commits O and P.
Discussions

git log --graph does not show branches, it only shows them as commits on master

Only the Gods

The great historian was trying to unravel the intricacies of an incorrect merge that had happened many months ago. He made a pilgrimage to Master Git to ask for his help.

"Master Git," said the historian, "what is the nature of history?"

"History is immutable. To rewrite it later is to tamper with the very fabric of existence."

The historian nodded, then asked: "Is that why rebasing commits that have been pushed is discouraged?"

"Indeed," said Master Git.

"Splendid!" exclaimed the historian. "I have a historical record of a merge commit with two parents. How can I find out which branch each parent was originally made on?"

"History is ephemeral," replied Master Git, "the knowledge you seek can be answered only by the gods."

The historian hung his head as enlightenment crushed down upon him.

From https://stevelosh.com/blog/2013/04/git-koans/

Branches are simply moving pointers in the graph of all commits. They can be added and removed without consequence and there is no record of their being there.

More on reddit.com
🌐 r/git
7
6
March 3, 2021
git branch - How to read a Git log graph - Stack Overflow
In the Git community book, it says Another interesting thing you can do is visualize the commit graph with the '--graph' option, like so: $ git log --pretty=format:'%h : %s' --graph * 2d3acf9 : i... More on stackoverflow.com
🌐 stackoverflow.com
algorithm - How does 'git log --graph' or 'hg graphlog' work? - Stack Overflow
I know that the history in Git is stored in a data structure called a DAG. I've heard about DFS and know it's somewhat related. I'm curious, how do programs such as git log --graph or hg graphlog ... More on stackoverflow.com
🌐 stackoverflow.com
[GIT] How to read the git log graphs?
Open the repo in gitk and it will show the same graph using actual graphics instead of ASCII. But essentially: The top is the most recent, the bottom is the oldest, so read it from bottom up (unless you specify --reverse.) Branches are vertical lines. The asterisk tells you which branch the current commit line is referring to. Where two branches come together or converge (i.e.: |\), that's a merge. Where they split or diverge (i.e.: |/), that's where the branch was created/taken off of. Therefore, to read the development of a branch, start with where it splits off, read the log entries for any lines corresponding to a * in that branch's vertical line, and follow it until it merges again. Note that not every log entry will be relevant, only those with a * on the branch in question. Sometimes branches are taken from far back, which means they have to 'jump over' a lot of commits when they're merged. More on reddit.com
🌐 r/learnprogramming
3
2
December 13, 2011
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-use-the-git-log-graph-command
How to use the git log graph and tree command
October 29, 2020 - The git log graph command creates a graphic overview of how a developer’s various development pipelines have branched and merged over time.
🌐
Prsantos
prsantos.com › posts › 2021-03-07-git-log-graph
Peter R. Santos - Git log graph
March 7, 2021 - I get a slightly prettier graph. By slightly prettier, I mean it has more information. I get the following additional information: How long ago that commit was. The author of the commit. The more I look at it though, I'm starting to lean towards the default git log --all --decorate --oneline --graph.
🌐
Reddit
reddit.com › r/git › git log --graph does not show branches, it only shows them as commits on master
r/git on Reddit: git log --graph does not show branches, it only shows them as commits on master
March 3, 2021 -

A time ago, at the institution I work for, the log graph of git used to show the commits in each branch before merging them into master, but now when we merge a branch it only shows them as commits on master, and does not show the history of the branches This did not happen before. Why is it happening this way? Thank you

* commit 
| 
| 
| 
|    
|  
* commit 
| 
| 
| 
|    
|  
* commit 
| 
| 
| 
|     
|  
* commit 
| 
| 
| 
|     
|  
* commit 
| 
| 
| 
|  
|  
* commit 
| 
| 
| 
|     
|  
* commit
| 
| 
| 
| 
| 
* commit
| 
| 
| 
|    
|  
* commit 
| 
| 
| 
|     
|  
* commit 
| 
| 
| 
|     
|  
* commit 
| 
| 
| 
|     
|  
* commit 
| 
| 
| 
|     
|  
* commit
| 
| 
| 
|    
|  
* commit
| 
| 
| 
|     
|    
*   commit 
|\  
| | 
| | 
| | 
| |     
| |   
| * commit 
| | 
| | 
| | 
| |     
| |   
* | commit 
|/  
|   
|   
|       
|  
* commit 
| 
| 
| 
|     
|    
*   commit
|\  
| | 
| | 
| | 
| |    
| |     
| *   commit
| |\  
| | | 
| | | 
| | | 
| | |     
| | |    
| | * commit
| | | 
| | | 
| | | 
| | |     
| | |    
| | * commit
| | | 
| | | 

https://drive.google.com/file/d/1pDMbZGDWdLeOBu2tW2R2-F00O8rGta2C/view

🌐
GitHub
github.com › orgs › community › discussions › 10515
Git Graph to see all commits across all branches · community · Discussion #10515
We need the ability to view all the branches (as labels) and their commits as graph. Equivalent git command: git log --graph --oneline --all
Find elsewhere
🌐
GitHub
gist.github.com › miebach › 6b33a9a83057bf055102
pretty git log graph with coloured branches · GitHub
pretty git log graph with coloured branches. GitHub Gist: instantly share code, notes, and snippets.
Top answer
1 of 4
8

First, one obtains a list of commits (as with git rev-list), and parents of each commit. A "column reservation list" is kept in memory.

For each commit then:

  • If the commit has no column reserved for it, assign it to a free column. This is how the branch heads will start.
  • Print the tree graphics according to the column reservation list, and then the commit message
  • The reservation's list entry for the current column/commit is updated with the first parent of the current commit, such that the parent is going to be printed in the same column.
  • Other parents get a new free column.
  • If this was a merge, the next line will try to link the second parent to a column where the commit is expected (this makes for the loops and the "≡ bridge")

Example showing output of git-forest on aufs2-util with an extra commit to have more than one branch).

With lookahead, one can anticipate how far down the merge point will be and squeeze the wood between two columns to give a more aesthetically pleasing result.

2 of 4
5

I tried looking around Git or hg's code but it's very hard to follow and get a general idea of what's going on.

For hg, did you try to follow the code in hg itself, or in graphlog?

Because the code of graphlog is pretty short. You can find it in hgext/graphlog.py, and really the important part is the top ~200 lines, the rest is the extension's bootstrapping and finding the revision graph selected. The code generation function is ascii, with its last parameter being the result of a call to asciiedge (the call itself is performed on the last line of generate, the function being provided to generate by graphlog)

🌐
Cuda-chen
cuda-chen.github.io › git › 2021 › 08 › 14 › git-pretty-branch-graph.html
How to Show Git Branch Graph in Terminal
August 14, 2021 - As a Git user, I can watch the Git commits by typing git log. However, sometimes I want to watch the branch graph so that I can know which branch merges to another branch.
🌐
CoreUI
coreui.io › answers › how-to-view-git-log-graph
How to view Git log graph · CoreUI
November 25, 2025 - Use git log --graph to display an ASCII art representation of branch structure and commit relationships in your repository.
🌐
Linux Kernel
kernel.org › pub › software › scm › git › docs › git-log.html
git-log(1)
August 25, 2025 - $ git rev-list --left-right --boundary --pretty=oneline A...B >bbbbbbb... 3rd on b >bbbbbbb... 2nd on b <aaaaaaa... 3rd on a <aaaaaaa... 2nd on a -yyyyyyy... 1st on b -xxxxxxx... 1st on a ... Draw a text-based graphical representation of the commit history on the left hand side of the output.
🌐
Microsoft Developer
developer.microsoft.com › en-us › graph
Microsoft Graph Dev Center | APIs and app development
Bicep and Terraform templates make it easy for you to deploy Microsoft Graph resources throughout your development lifecycle as part of your infrastructure-as-code solution.
🌐
grep Flags
zwischenzugs.com › 2016 › 06 › 04 › power-git-log-graphing
Power ‘git log’ graphing – zwischenzugs.com
June 4, 2016 - git log --graph --oneline --all --decorate --simplify-by-decoration --pretty='%ar %s %h'
🌐
tech.serhatteker.com
tech.serhatteker.com › post › 2021-02 › git-log-tree
Visualize Git Log Tree — tech.serhatteker.com
February 4, 2021 - $ git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --all
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Git Graph - Visual Studio Marketplace
Extension for Visual Studio Code - View a Git Graph of your repository, and perform Git actions from the graph.