How to Use git log: View Commit History Easily
Use
git log to see the list of commits in your Git repository. It shows commit IDs, authors, dates, and messages. You can add options like --oneline for a shorter view or --graph to see a visual branch structure.Syntax
The basic syntax of git log is simple and flexible:
git log: Shows the full commit history.git log [options]: Adds filters or changes output format.git log [branch]: Shows commits for a specific branch.
Common options include --oneline for a brief summary, --graph for a visual tree, and --author=<name> to filter by author.
bash
git log [options] [branch]Example
This example shows how to use git log with options to get a clear, short view of the commit history with a graph:
bash
git log --oneline --graph --decorate --allOutput
* 9fceb02 (HEAD -> main, origin/main) Fix typo in README
* 3a1b2c4 Add user login feature
* 7d8e9f0 Update dependencies
* 5f6a7b8 Initial commit
Common Pitfalls
Beginners often face these issues:
- Running
git logwithout options can show too much information, making it hard to read. - Not using
--onelineor--graphwhen wanting a quick overview. - Confusing commit hashes by copying incomplete IDs; always use at least 7 characters.
Correct usage example:
bash
git log --oneline # Wrong: git log --oneline --graph --author # Missing author name after --author option # Right: git log --oneline --author="Alice"
Quick Reference
| Option | Description |
|---|---|
| --oneline | Show each commit as one line with short hash and message |
| --graph | Display an ASCII graph of the branch structure |
| --decorate | Show branch and tag names next to commits |
| --author= | Filter commits by author name |
| -n | Show only the last |
| --since= | Show commits more recent than a date |
| --stat | Show files changed with each commit |
Key Takeaways
Use
git log to view commit history with details like author and date.Add
--oneline and --graph for a concise and visual commit list.Filter commits by author or date using options like
--author and --since.Always use at least 7 characters of the commit hash to avoid confusion.
Customize output with options to find the information you need quickly.