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.
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:
A more detailed format with timestamps and authors:
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.
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:
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-parentin Git when you only want mainline merge history
Example:
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 --graphandhg log -Gvisualize the commit DAG.- The output reflects parent links, not just commit time.
- Use
--oneline,--decorate, and--allfor practical day-to-day views. - Build a local demo repo to learn how lanes and merges appear.
- Add filters such as
--first-parentto answer specific history questions.
Related reading
- How does Git save space and is fast at the same time?
- How does Git store files?
- How does kafka consumer auto commit work?
- How does one iteratively write merge sort?
- How external merge sort algorithm works?
- How git works when two peers push changes to same remote simultaneously
- how I can synchronized the airflow dags repository in github with an azure storage account?
- How implement Rollback feature?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.