[Git Basics] How to Use Git log: Mastering Commit History

VnnTools

In the world of programming, Git is like a diary that records every change and step in your project. And if Git is the diary, then git log is the "time machine" that lets you travel to the past, see who did what, when, and why.

How to Use Git log: Mastering Commit History

However, for many, the default git log looks like an unreadable mess. Don’t worry! This article will help you turn git log from a scary tool into your most powerful companion, helping you debug, analyze, and manage your project like a pro. ✨

1. First Steps: The Raw Git log

When you type git log and hit Enter, you’ll see a list of commits in reverse chronological order. Each log entry usually includes:

  • Commit hash: A long, unique string—like an ID card for each commit.
  • Author: Who made the change.
  • Date: When the change was made.
  • Commit message: A note explaining the change.
git log

Example output:

commit a89c4d516e256f0c624c878a1e2f5756910e5a6d (HEAD -> main)
Author: An Nguyen <an.nguyen@email.com>
Date:   Tue Aug 19 14:20:10 2025 +0700

    feat: Add user authentication feature

commit 3b3e1b8b3e3e4b3e5b3e6b3e7b3e8b3e9b3ea1b1
Author: Binh Tran <binh.tran@email.com>
Date:   Mon Aug 18 10:05:30 2025 +0700

    fix: Correct calculation error in payment module

Looks a bit overwhelming, right? Let’s make it prettier!

2. Beautifying the Output: Making git log Clean and Readable 🎨

Here are the most common options to make git log output much more user-friendly.

View a Short Version with --oneline

Adding --oneline condenses each commit to a single line, showing only the short hash and the commit title.

git log --oneline

Result:

a89c4d5 (HEAD -> main) feat: Add user authentication feature
3b3e1b8 fix: Correct calculation error in payment module

Much cleaner!

Visualize History with --graph

When your project has multiple branches and merges, history can get complicated. --graph draws an ASCII diagram to visualize branches and merges, making the project’s flow clear.

Combine it with --oneline for a perfect "history picture."

git log --graph --oneline --all

Note: --all shows the history of all branches, not just the current one.

Customize Output with --pretty=format

This is where you show your power! --pretty=format lets you format the output as you like with special placeholders:

  • %h: Short hash
  • %H: Full hash
  • %s: Commit subject
  • %an: Author name
  • %ar: Relative date (e.g., "2 hours ago")
  • %ad: Author date
  • %d: Reference pointers (e.g., (HEAD -> main, origin/main))

Example of a fancy format:

git log --pretty=format:"%h - %an, %ar : %s"

Result:

a89c4d5 - An Nguyen, 2 hours ago : feat: Add user authentication feature
3b3e1b8 - Binh Tran, 1 day ago : fix: Correct calculation error in payment module

3. Filtering History: Become a Detective 🔍

When your project’s history is thousands of commits long, viewing everything is impossible. Here’s where filtering and searching skills shine.

Filter by Number of Commits

Want to see only the 5 most recent commits? Easy!

git log -n 5
# or
git log -5

Filter by Date

Find commits made within a certain time frame.

# Commits from the last 2 weeks
git log --since="2 weeks"

# Commits before August 15, 2025
git log --until="2025-08-15"

# Combine: Commits by 'An Nguyen' from 1/8 to 15/8
git log --author="An Nguyen" --since="2025-08-01" --until="2025-08-15"

Filter by Author

See contributions from a specific team member.

git log --author="Binh Tran"

Filter by Commit Message Content

Remember a commit about "payment" but not the details? --grep helps you find it.

git log --grep="payment"

Note: By default, --grep is case-sensitive. Add -i for case-insensitive search (--grep="payment" -i).

Filter by File Changes

One of the most powerful features! Who changed header.component.css?

git log -- header.component.css

Filter by Commit Range

See commits present in the feature branch but not in main.

git log main..feature

4. Handy Recipes: Real-World Scenarios 🚀

Combine the above commands to solve common problems.

  • Recipe 1: View a file’s history with patch details

    git log -p --follow -- <file-name>
    

    -p shows the diff for each commit. --follow tracks history even if the file was renamed.

  • Recipe 2: See who changed a specific line of code While git blame is better for this, git log can also help.

    git log -S "code snippet you’re looking for" --source --all
    

    -S searches for commits that added or removed a string.

  • Recipe 3: Create an alias for your favorite log command Typing a long command repeatedly is tiring. Create a Git alias!

    git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --abbrev-commit"
    

    Now, just type git lg for a beautiful output!

Conclusion: Git log Is More Than Just a Command

git log is not just a command to review history. It’s a diagnostic tool, an analytical assistant for your project. By combining formatting, filtering, and searching options, you can quickly find exactly the information you need.

Don’t be afraid to experiment with different options. The more you practice, the more powerful and useful git log will become.

Good luck mastering Git!

Related Posts

[Git Basics] What is a Git Branch? Mastering Branches in Git

Branching is a crucial feature that helps split projects in Git. Read this article to understand Git Branch, how to create, delete, and switch between branches easily.

[Git Basics] What is Git? Discover the Role of Git in Programming

Git is an extremely popular version control system. This article explains what Git is, why every developer should know it, and the steps to get started.

[Git Basics] How to Install and Configure Git Initially: What You Need to Know

Getting started with Git has never been easier! A detailed guide on how to install and configure Git for the first time, helping you set up an efficient working environment in just a few minutes.

[Git Basics] What is the .gitignore File? How to Use It Effectively for Every Project

Learn how to use the .gitignore file to exclude temporary files, logs, and unnecessary folders. Simplify your project and keep your Git repository clean.