The git log
command is incredibly powerful, offering a multitude of options to help you understand what work has been done in your repositories and who has done it. This guide highlights some of the most useful git log
options, many of which are sourced from Scott Chacon’s Gitcast on git log
and R. Tyler Ballance’s Git Protip series.
Useful git log
Options
Show a Specific Number of Commits
git log -<n>
Displays the log for the last n
commits. For example, git log -2
shows the last two commits.
Display with File Changes and Insertions/Deletions
git log --stat
Includes the number of files changed, and the insertions and deletions for each commit. It essentially appends the git commit
output to each message.
Show Add/Modify/Delete Status
git log --name-status
Displays an SVN-like status of added, modified, and deleted files for each commit. It’s basic but provides a clear overview of what’s changed.
Compress Log Output
git log --pretty=oneline
Condenses each commit to its SHA1 hash and message on a single line. Pipe this to wc -l
to count the commits.
View Unpushed Commits
git log origin..HEAD
Lists commits that have not yet been pushed to the origin remote. (Thanks to Ryan Bates for this tip!)
View Commits for a Specific File
git log <file>
Displays all commits that have affected the specified file.
Filter Commits by Author and Exclude Merges
git log --no-merges --author=<name>
Shows all commits by the specified author and excludes merge commits to reduce noise.
View Commits Since a Specific Date
sh
git log --since="1 week ago"
Displays commits made since the specified date. You can replace “1 week ago” with other time frames like “yesterday,” “1 year ago,” “3 months ago,” or a specific date like “1/20/09.” Other time-based options include --after
, --until
, and --before
for more flexibility.
Search Commit Messages with a Regular Expression
git log --grep='<pattern>'
Searches through commit messages to find those matching the specified pattern. For example, git log --grep='^Bump'
finds commits with messages starting with “Bump.” This can be particularly useful if you only remember part of a commit message.
Disable Paging for Log Output
git --no-pager log
Displays the log output without using less
or any pager. This option is useful if you prefer straight output.
Explore More Options
For a comprehensive list of available options, refer to the git log
manpage. If you have a favorite git log
tip that wasn’t mentioned here, share it in the comments, and we’ll update this guide.
By mastering these git log
options, you can efficiently navigate your repository’s history, track changes, and gain insights into your project’s evolution.