git log — Advanced History
You've already used git log to see commits. Now let's explore its powerful options for filtering and formatting.
Useful Flags
git log --oneline— Compact one-line formatgit log -n 3— Show only the last 3 commitsgit log --graph— ASCII art showing branch structuregit log --all— Show commits from all branches, not just current
Combining Flags
The real power comes from combining flags:
git log --oneline --graph --all
This gives you a compact visual overview of your entire repository — branches, merges, and all.
Try it!
Build up a history with branches, then explore it:
- Make three commits on main:
echo "v1" > app.txt && git add app.txt && git commit -m "Version 1"echo "v2" > app.txt && git add app.txt && git commit -m "Version 2"echo "v3" > app.txt && git add app.txt && git commit -m "Version 3"
- View compact log:
git log --oneline - View only last 2:
git log --oneline -n 2
You should see 3 commits with their short SHAs and messages.
Reading the Output
a1b2c3d (HEAD -> main) Version 3
e4f5g6h Version 2
i7j8k9l Version 1
- The SHA prefix (
a1b2c3d) uniquely identifies each commit (HEAD -> main)shows where you currently are- Messages appear in reverse chronological order (newest first)