My favourite git log command 😃
1. How do I view commit logs?
The answer is git log, below from the Git SCM online manual:
git log [<options>] [<revision-range>] [[--] <path>…]
Git log is one of the most powerful tools for exploring your repository’s history. It shows commit history in reverse chronological order (newest first) and can be customized extensively to show exactly the information you need.
2. Easy favourite
There are many variations I do like but my favourite is this simple beauty:
git log --oneline --graph --decorate --all
This command gives you a compact, visual representation of your entire repository’s commit history across all branches. It’s perfect for understanding the branching structure and seeing how different features were merged.
Pro tip: You can create a Git alias for this:
git config --global alias.lg "log --oneline --graph --decorate --all"
# Now just use: git lg
3. What are these options?
--oneline
- This is a shorthand for
--pretty=oneline --abbrev-commitused together - Shows each commit on a single line with abbreviated commit hash
- Perfect for getting an overview without overwhelming detail
--graph
- Draw a text-based graphical representation of the commit history on the left hand side of the output
- Shows branching and merging patterns visually using ASCII characters
- This may cause extra lines to be printed between commits for proper graph drawing
- Cannot be combined with
--no-walk - This enables parent rewriting and implies
--topo-orderoption by default
--decorate[=short|full|auto|no]
- Print out the ref names (branch names, tags) of any commits that are shown
- short: ref name prefixes
refs/heads/,refs/tags/andrefs/remotes/will not be printed - full: the full ref name (including prefix) will be printed
- auto: if output goes to a terminal, shows as if
shortwere given, otherwise no ref names - The option
--decorateis short-hand for--decorate=short - Default to configuration value of
log.decorateif configured, otherwiseauto
--all
- Pretend as if all the refs in
refs/, along withHEAD, are listed on the command line as<commit> - This means you see commits from ALL branches, not just the current one
- Essential for getting the complete picture of your repository’s history
4. Output => pretty
* eb8b529 (HEAD -> main) adding new blog
* 46d6a08 (origin/main) Merge pull request
|\
| * 02f3939 (origin/test)
|/
* 58fa110 Merge pull request
|\
| * 84f0ae0 removed
* | 326c568 Merge pull request
|\|
| * d7df919 updated
|/
Reading the graph:
*represents commits|and/and\show branch relationships(HEAD -> main)shows where you currently are(origin/main)shows the remote branch position- Merge commits create the branching patterns you see
Other useful git log variations:
# Show the last 10 commits with more detail
git log --oneline -10
# Show commits from a specific author
git log --author="Your Name" --oneline
# Show commits in a date range
git log --since="2 weeks ago" --until="1 week ago" --oneline
# Show commits that changed a specific file
git log --oneline -- path/to/file.txt
# Show commit stats (files changed, insertions, deletions)
git log --stat --oneline
Why am I doing this? Learn one new thing everyday. 👍