git log
Show commit logs
The 'git log' command shows the commit history of your repository. It displays commits in reverse chronological order (newest first), showing commit hash, author, date, and message. With various options, you can customize the output format, filter commits, and follow file history. Essential for understanding project history and finding specific changes.
Synopsis
git log [<options>] [<revision-range>] [[--] <path>...]Options
Choosing the Right Option
Log Output Formats
Choose the right format for your needs
| Command | Shows | Verbosity | Best For |
|---|---|---|---|
git log | Full details | High | Detailed review |
git log --oneline | Hash + message | Low | Quick overview |
git log --stat | Files changed per commit | Medium | See scope of changes |
git log -p | Full diff | Very high | Code review |
Filtering Commits
Find specific commits in history
| Command | Filter By | Example |
|---|---|---|
--author | Person who wrote the commit (author; use --committer for committer) | --author="John" |
--since/--until | Date range | --since="2024-01-01" |
--grep | Commit message | --grep="fix" |
-- <path> | Files changed | -- src/app.js |
Log vs Reflog
Understanding when to use each
| Command | Shows | Survives Reset? | Use For |
|---|---|---|---|
git log | Commit history on branch | No | Normal history viewing |
git reflog | All HEAD movements | Yes | Recovering lost commits |
Examples
View recent commits
git logShows full commit history with hash, author, date, and message.
Compact one-line view
git log --onelineShows each commit on one line with short hash and message.
View file history
git log -- path/to/file.jsShows only commits that changed the specified file.
Visual branch graph
git log --oneline --graph --allShows a text-based graph of all branches and their commits.
Workflow Examples
git log --oneline -10Quick view of last 10 commits
git log -p -1See full diff of most recent commit
git log --oneline -- path/to/file.jsList commits that changed this file
git log -p -- path/to/file.jsSee the actual changes made
git blame path/to/file.jsSee who wrote each line
git log --oneline --graph --allFull branch visualization
git log --oneline --graph main..featureCommits in feature not in main
Troubleshooting
Log output is too long / can't exit
Cause: Git uses 'less' pager by default
Press 'q' to quit. Use -n to limit commits, or pipe to head: git log --oneline | head -20
Don't see commits from other branches
Cause: Git log only shows commits reachable from HEAD (your current branch) by default
Use --all flag: git log --oneline --all
Can't find a commit I know exists
Cause: Commit might be on different branch or was reset
Try git reflog to see all recent HEAD positions, including after resets
Log shows too much / too little information
Cause: Default format might not suit your needs
Use --oneline for brief, -p for detailed, --stat for file summary
Tips & Pitfalls
Best Practices
- ✓Use --oneline for quick overviews, full log for detailed review
- ✓Combine --graph --all --oneline for a complete branch visualization
- ✓Use -n to limit output when you only need recent commits
- ✓Filter by author, date, or path to find specific changes
- ✓Use git log main..feature to see commits unique to a branch
Common Mistakes
- ✗Not knowing you can press 'q' to quit the log viewer (uses less pager by default).
- ✗Forgetting --all when wanting to see commits from all branches, not just current.
- ✗Using git log when you need git reflog - log shows commit history, reflog shows all ref changes including resets.
- ✗Not using -- before file paths, which can cause ambiguity with branch names.
FAQ
Press 'q' to quit. Git log uses the 'less' pager. You can also use -n flag to limit output or pipe to head.
Use --all flag: git log --oneline --all. Without it, you only see commits reachable from your current branch (HEAD).
Use -p (patch) flag: git log -p. Add -1 to see just the most recent: git log -p -1
Use --author: git log --author="name". Matches partial names and emails (case-sensitive; add -i to ignore case).
Log shows commit history. Reflog shows all HEAD movements including resets. Use reflog to recover 'lost' commits.