git log
hg graphlog
version control
Git
Mercurial

How does 'git log --graph' or 'hg graphlog' work?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

git log --graph and Mercurial graph views draw commit history as a directed acyclic graph, not a simple linear list. The graph helps you see branch divergence, merges, and commit ancestry quickly. Understanding how the graph is built makes the output far easier to read during debugging and release work.

Commit History Is a Graph

Each commit points to one parent, or multiple parents for merge commits. A graph tool prints commits in topological order and then draws connectors to represent parent relationships.

In Git, a normal commit has one parent and a merge commit has two or more. The graph renderer keeps track of active branch lanes and reuses columns as branches start and end.

Useful Git Command Patterns

Start with a compact, readable view:

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

A more detailed format with timestamps and authors:

bash
git log --graph --decorate --all \
  --pretty=format:'%C(auto)%h %d %s %Cgreen(%cr) %Cblue<%an>'

The graph symbols are simple:

  • * marks a commit
  • | continues a lane
  • / and \ show merges or splits

Reproducible Example Repository

You can create a tiny repository and observe the graph behavior directly.

bash
1mkdir graph-demo
2cd graph-demo
3git init
4echo "base" > app.txt
5git add app.txt
6git commit -m "base commit"
7
8git checkout -b feature-a
9echo "a1" >> app.txt
10git commit -am "feature a work"
11
12git checkout master
13git checkout -b feature-b
14echo "b1" >> app.txt
15git commit -am "feature b work"
16
17git checkout master
18git merge --no-ff feature-a -m "merge feature a"
19git merge --no-ff feature-b -m "merge feature b"
20
21git log --graph --oneline --decorate --all

After the two merges, you will see multiple lanes join the main line.

How Mercurial Graphlog Compares

Mercurial has similar graph output through extensions or built-in command variants depending on version and tooling. A common pattern is:

bash
hg log -G

Conceptually it is the same idea: show commits, parents, and branch relationships using ASCII lines. The rendering details differ, but both Git and Mercurial are exposing commit DAG structure.

Reading Graphs Efficiently

A practical reading strategy:

  • Identify the commit you care about by hash or tag
  • Follow lines upward to locate parents and merged branches
  • Check decorations to see branch names and tags
  • Use --first-parent in Git when you only want mainline merge history

Example:

bash
git log --graph --oneline --decorate --first-parent

This is especially useful for release notes and deployment audits.

Performance and Scaling Tips

Large monorepos can produce graph output that is hard to parse and slow to render. Narrow the scope with branch names, date windows, or path filters when investigating a specific regression. For example, run git log --graph --oneline -- src/payments to inspect only commits that touched one subsystem. This keeps the visual model compact and makes parent relationships easier to reason about during incident response.

Common Pitfalls

  • Assuming leftmost lane is always the most important branch
  • Forgetting --all, then missing commits from non-current branches
  • Misreading chronological order as ancestry order
  • Ignoring decorations, which leads to confusion around branch tips
  • Overusing graph view on very large histories without filters

When history is large, combine graph output with path filters, date limits, or branch constraints to keep it readable.

Summary

  • git log --graph and hg log -G visualize the commit DAG.
  • The output reflects parent links, not just commit time.
  • Use --oneline, --decorate, and --all for practical day-to-day views.
  • Build a local demo repo to learn how lanes and merges appear.
  • Add filters such as --first-parent to answer specific history questions.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.