Git Blame & Log: Who Changed What?
Git Blame
git blame shows who last modified each line of a file, and when:
git blame index.html
Output:
a1b2c3d4 (Alice 2024-01-15) <html>
a1b2c3d4 (Alice 2024-01-15) <head>
e5f6g7h8 (Bob 2024-02-20) <title>My App</title>
a1b2c3d4 (Alice 2024-01-15) </head>
i9j0k1l2 (Charlie 2024-03-10) <body class="dark">
When to Use Blame
- Understanding code: Who wrote this? What was the context?
- Finding bugs: Who introduced this change?
- Code review: When was this last modified?
Blame Options
git blame file.txt # Full blame
git blame -L 10,20 file.txt # Only lines 10-20
git blame -w file.txt # Ignore whitespace changes
git blame --since="2024-01-01" # Changes after date
Git Log
git log shows the commit history. It's your project's diary.
# Basic log
git logCompact one-line format
git log --onelineWith graph visualization
git log --oneline --graph --allFilter by author
git log --author="Alice"Filter by date
git log --since="2024-01-01" --until="2024-06-01"Filter by file
git log -- path/to/file.jsSearch commit messages
git log --grep="fix bug"
Git Show
View the details of a specific commit:
git show abc1234
This shows:
- The commit message
- The author and date
- The exact changes (diff)
Practical Investigation
Here's how experienced developers track down issues:
# 1. Find when a file was last changed
git log --oneline -- src/login.js2. See who modified line 42
git blame -L 42,42 src/login.js3. View that commit's full details
git show abc12344. See what else changed in that commit
git show --stat abc1234
Summary
| Command | Purpose |
|---|---|
| <code>git blame file</code> | Show who changed each line |
| <code>git blame -L 10,20 file</code> | Blame specific lines |
| <code>git log --oneline</code> | Compact commit history |
| <code>git log --graph --all</code> | Visual branch history |
| <code>git show HASH</code> | Details of one commit |
| <code>git log --author="name"</code> | Filter by author |