How to Use git log --graph to Visualize Commit History
Use
git log --graph to display your commit history as a text-based graph showing branches and merges. Combine it with options like --oneline and --all for a compact and complete view of all branches.Syntax
The basic syntax for using the graph option in git log is:
git log --graph: Shows commit history as a graph.--oneline: Displays each commit in one line for brevity.--all: Includes all branches in the graph.--decorate: Adds branch and tag names to commits.
bash
git log --graph [--oneline] [--all] [--decorate]Example
This example shows a simple git log graph with one line per commit, including all branches and decorations for branch names.
bash
git log --graph --oneline --all --decorateOutput
* 9fceb02 (HEAD -> main, origin/main) Fix typo in README
| * 7ac9a67 (feature) Add new feature
|/
* 3a1b2c4 Initial commit
Common Pitfalls
Common mistakes when using git log --graph include:
- Not using
--alland missing commits from other branches. - Omitting
--onelinewhich makes the output very long and hard to read. - Ignoring
--decoratewhich helps identify branches and tags.
Always combine these options for the clearest graph view.
bash
git log --graph # vs # Better: git log --graph --oneline --all --decorate
Quick Reference
| Option | Description |
|---|---|
| --graph | Draws a text-based graph of the commit history |
| --oneline | Shows each commit as a single line |
| --all | Shows commits from all branches |
| --decorate | Shows branch and tag names next to commits |
Key Takeaways
Use
git log --graph --oneline --all --decorate for a clear visual of all branches and commits.The
--graph option draws the commit tree with lines connecting branches and merges.Adding
--oneline makes the output concise and easier to read.Include
--all to see commits from every branch, not just the current one.Use
--decorate to identify branch and tag names in the log.