Git Log Tips: Find Commits Easily and Quickly
git log;
Following my last blog you can access below;
Today I will share some Git log commands that will be helpful to you in your VCS journey.
If you’ve read my previous article, you’re already familiar with Git and its usage. For those who haven’t, Git log is a powerful command that allows you to review the commit history in a Git repository and offers various options to filter out the commit messages based on your needs.
Now without any further delay, let’s jump to the git log options
Let’s dive into git log options!
Log all
First and foremost, the Git log command is used to list all the commit messages of the branch including the information about the commit author and date of commit along with the commit hash and message.
# log all commits
git log
If you don’t know what branch is, don’t worry we will discuss it in upcoming articles.
Filter the Commits
Get n commits
You are least likely to use this option since by default Git provides only the commit messages that fit into your terminal. But if you anyhow still want to get a specific number of commits only you can do so by using -n
option.
# show the last 5 commits
git log -n 5
Filter commits by Date
You can also filter commits based on when they are committed. Git provides you with the option to filter the commits after the specified date using --since
, --after
, and to get the commits before the specified date using --until
, and --before
.
Also, these commands work with both specific and relative dates.
# show commits from the past two weeks
git log --since="2 weeks ago"
# or by using --after
git log --after="2 weeks ago"
# show commits until January 1, 2024
git log --until="2024-01-01"
# or by using --before
git log --before="2024-01-01"
It is also recommended that for consistent behavior you specify the timezone git log --date=local ...
Filter by Author
Sometimes you might want to filter the commits by author. I mostly need it to filter out my commits before the scrum meeting to prepare my status. But hey, it also has many good uses, which obviously doesn’t include stalking another dev.
# get commits from a specific author
git log --author="Author Name"
Filter Commits based on Commit Messages
This is the most useful filter option that comes with Git log. If you follow a good commit message convention then this would greatly help you to filter specific types of commits like fix, build, ci, etc.
# show commits with "fix" in the message
git log --grep="fix"
Formatting Output
You can also format the log messages as it suits you by using the --pretty
option.
Predefined Formats
oneline
: Displays each commit on a single line.short
: Includes commit hash, author, date, and subject.medium
: Adds commit message and file changes.full
: Includes full commit information.format
: Allows custom formatting using placeholders (e.g.,%H
for commit hash,%an
for the author name,%s
for the subject).
# show concise logs
git log --pretty=oneline
# use --oneline for combining --pretty=oneline --abbrev-commit
git log --oneline
# custom format with short hash and subject
git log --pretty=format:"%h - %s"
Custom Formatting
Or you can create custom formatting by utilizing specifiers.
git log --pretty=format:"Commit: %H\nAuthor: %an\nDate: %ad\nSubject: %s\n\n%b"
Git log useful Specifiers
%H
Commit hash
%h
Abbreviated commit hash
%T
Tree hash
%t
Abbreviated tree hash
%P
Parent hashes
%p
Abbreviated parent hashes
%an
Author name
%ae
Author email
%ad
Author date (format respects the --date=option)
%ar
Author date, relative
%cn
Committer name
%ce
Committer email
%cd
Committer date
%cr
Committer date, relative
%s
Subject
Viewing Commit Details
-p
: Show the patch (diff) introduced by each commit.--stat
: Show statistics for each commit (added, deleted, modified files).--shortstat
: Show a summary of added, deleted, and modified files.
git log -p --stat
Other Useful Options
--graph
: Display a commit graph.--follow
: Continue listing the history of a file beyond renames.--reverse
: Show commits in chronological order (oldest first).--no-merges
: Exclude merge commits.--first-parent
: Only follow the first parent commit of merge commits.
Combining Options
As I have mentioned above about --oneline
, you can combine multiple options.
# get commits from "Author Name" formatted as "%h - %s" commited within a month
git log --author="Author Name" --since="1 month ago" --pretty=format:"%h - %s"
# find bug fix commits from last week
git log --grep="fix" --since="1 week ago" --pretty=format:"%h - %s"
Advanced Usage
Commit Ranges: Specify a range of commits using
commit1..commit2
syntax.Refspecs: Use refspecs like
origin/master..HEAD
to compare branches.Filtering by File: Use
--path
to filter commits based on file paths.Interactive Rebasing: Use
git rebase -i
to edit commit history.Cherry-Picking: Use
git cherry-pick
to apply specific commits to another branch.
Reference:
Subscribe to my newsletter
Read articles from Samar Jaffri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Samar Jaffri
Samar Jaffri
A passionate ML Engineer, loves to learn, code and share insights with the 🌍