0
0
GitHow-ToBeginner · 3 min read

How to Use Git Log for Debugging: Simple Guide

Use git log to view the history of commits in your project, which helps identify when and where bugs were introduced. By examining commit messages and changes, you can trace back to the exact commit causing issues and understand the code evolution for effective debugging.
📐

Syntax

The basic syntax of git log is simple but powerful. You can customize it with options to filter and format the commit history.

  • git log: Shows the full commit history.
  • git log -p: Shows the patch (code changes) introduced in each commit.
  • git log --author=<name>: Filters commits by author.
  • git log --since=<date>: Shows commits after a specific date.
  • git log --grep=<keyword>: Searches commit messages for a keyword.
bash
git log [options]

# Examples:
git log

git log -p

git log --author="Alice"

git log --since="2 weeks ago"

git log --grep="fix bug"
💻

Example

This example shows how to use git log -p to see the exact code changes in recent commits, helping you find where a bug was introduced.

bash
git log -p -2
Output
commit 9fceb02d0ae598e95dc970b74767f19372d61af8 Author: Alice <alice@example.com> Date: Tue Apr 23 14:00 2024 +0000 Fix typo in README diff --git a/README.md b/README.md index e69de29..b7a8f3c 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -Hello world +Hello world! + commit 7ac9a67d8f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c Author: Bob <bob@example.com> Date: Mon Apr 22 10:30 2024 +0000 Add new feature X diff --git a/app.py b/app.py index 1234567..89abcde 100644 --- a/app.py +++ b/app.py @@ -10,7 +10,8 @@ def feature_x(): - print("Old code") + print("New feature X code")
⚠️

Common Pitfalls

Beginners often miss useful options or get overwhelmed by too much output. Here are common mistakes:

  • Running git log without filters can show too many commits, making it hard to find relevant info.
  • Not using -p or --stat to see code changes hides what was actually changed.
  • Ignoring commit messages or authors can slow down debugging.

Use filters and formatting to focus on useful data.

bash
Wrong:
git log

Right:
git log -p --since="1 week ago" --author="Alice"
📊

Quick Reference

CommandDescription
git logShow full commit history
git log -pShow code changes (patch) in each commit
git log --author="name"Filter commits by author
git log --since="date"Show commits after a date
git log --grep="keyword"Search commit messages
git log --statShow summary of changes per file

Key Takeaways

Use git log to trace commit history and find when bugs were introduced.
Add -p to see exact code changes for better debugging insight.
Filter logs by author, date, or keywords to narrow down relevant commits.
Avoid overwhelming output by combining options to focus your search.
Read commit messages carefully to understand the purpose of changes.