close
close

Mastering Git Log: A Complete Guide

Mastering Git Log: A Complete Guide

git journal;

Following my last blog you can access below;

samarjaffri

A Beginner’s Guide to Git Commands

Samar F. Jaffri · July 26

#git
#github
#beginners
#Tutorial

I’m going to share some Git log commands that will be useful to you in your VCS journey.

If you have read my previous article, you already know about Git and how to use it. For those who have not read it, Git log is a powerful command that allows you to view the commit history in a Git repository and provides various options to filter commit messages based on your needs.

Now, without further ado, let’s move on to the git log options

Let’s dive into the git log options!

Save all

First of all, Git The log command is used to list all commit messages in the branch, including information about who made the commit and the date of the commit as well as the commit hash and message.

# log all commits 
git log 

If you don’t know what a branch is, don’t worry, we will discuss it in the next articles.

Filter commits

Get n commits

You are unlikely to use this option because by default Git only provides commit messages that match your terminal. But if you still want to get only a specific number of commits, you can do so using -n option.

# show the last 5 commits 
git log -n 5 

Filter commits by date

You can also filter commits based on the date they are committed. Git gives you the ability to filter commits after the specified date using --since, --afterand to get commits before the specified date using --untilAnd --before.

Additionally, these commands work with 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, for consistent behavior, to specify the time zone git log --date=local ...

Filter by author

Sometimes you may want to filter commits by author. I mostly need this to filter my commits before the Scrum meeting to prepare my status. But hey, it also has many interesting uses, which obviously don’t include stalking another developer.

# get commits from a specific author 
git log --author="Author Name" 

Filter commits based on commit messages

This is the most useful filtering option provided with git log. If you follow a good commit message convention, it will greatly help you filter specific types of commits like fix, build, ci, etc.

# show commits with "fix" in the message 
git log --grep="fix" 

Formatting the output

You can also format the log messages however you like using the --pretty option.

Predefined formats

  • oneline: Displays each commit on a single line.
  • short: Includes commit hash, author, date, and subject.
  • medium: Adds a commit message and file changes.
  • full: Includes full validation information.
  • format:Allows custom formatting using placeholders (e.g., %H for the validation hash, %an for the author’s 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 using specifiers.

git log --pretty=format:"Commit: %H\nAuthor: %an\nDate: %ad\nSubject: %s\n\n%b" 

Useful Git Log Specifiers

  • %H Validate the hash
  • %h Short validation hash
  • %T Tree hashing
  • %t Tree hash abbreviation
  • %P Parental Hashes
  • %p Shortened Parental Hashes
  • %an Name of the author
  • %ae Author’s Email
  • %ad Author date (format respects –date= option)
  • %ar Author date, relative
  • %cn Contributor Name
  • %ce Contributor Email
  • %cd Date of engagement
  • %cr Date of engagement, relative
  • %s Subject

Viewing engagement details

  • -p:Show the patch (diff) introduced by each commit.
  • --stat:Show statistics for each commit (files added, deleted, modified).
  • --shortstat:View a summary of added, deleted, and modified files.
git log -p --stat 

Other useful options

  • --graph:Show a validation graph.
  • --follow:Continue listing a file’s history beyond renames.
  • --reverse:Show commits in chronological order (oldest first).
  • --no-merges: Exclude merge commits.
  • --first-parent:Only track the first parent commit of merge commits.

Combination of options

As I mentioned above about --onelineyou can combine several 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

Validation Ranges: Specify a validation range using commit1..commit2 syntax.

Refspecs: Use refspecs as origin/master..HEAD to compare branches.

Filtering by file: usage --path to filter commits based on file paths.

Interactive Rebasing: Usage git rebase -i to modify the commit history.

Cherry selection: use git cherry-pick to apply specific commits to another branch.

Reference:

  • https://training.github.com/downloads/github-git-cheat-sheet/
  • https://git-scm.com/docs/git#_git_commands
  • https://git-scm.com/docs/giteveryday
  • Book: Pro Git