Git Blame

The git blame command shows, line by line, who last modified each line in a file, when it was changed, and in which commit the change was made.

Despite its name, git blame is not actually used to assign fault — it is a powerful investigative tool for understanding why a piece of code was written the way it was, tracking down the source of a bug, or finding context for unfamiliar code.

Real-life analogy: Think of a shared logbook in a team office where every person signs next to every entry they write. If a wrong entry causes a problem, the logbook shows exactly who wrote what and when — not to punish anyone, but to trace what happened, understand the reasoning, and fix it.

Basic Syntax

git blame <filename>

Understanding the Output

git blame index.html

Sample output:

a3f92b1c (Ravi Kumar   2023-10-09 14:32:10 +0530  1) <html>
a3f92b1c (Ravi Kumar   2023-10-09 14:32:10 +0530  2)   <head>
b7c3d45e (Priya Sharma 2023-10-08 11:15:00 +0530  3)     <title>My Website</title>
a3f92b1c (Ravi Kumar   2023-10-09 14:32:10 +0530  4)   </head>
c1a2b3c4 (Amit Verma   2023-10-07 09:00:00 +0530  5)   <body>
c1a2b3c4 (Amit Verma   2023-10-07 09:00:00 +0530  6)     <h1>Welcome</h1>
b7c3d45e (Priya Sharma 2023-10-08 11:15:00 +0530  7)   </body>
a3f92b1c (Ravi Kumar   2023-10-09 14:32:10 +0530  8) </html>
ColumnMeaning
a3f92b1cShort commit hash — the commit that last changed this line
(Ravi Kumar)Author who made the change
2023-10-09 14:32:10Date and time of the change
1)Line number in the file
<html>The actual content of the line

Blaming a Specific Range of Lines

To view blame only for lines 10 through 25 of a file:

git blame -L 10,25 index.html

To start from a specific line to the end of the file:

git blame -L 10, index.html

Using a function name as the range (Git detects the function boundaries):

git blame -L :functionName app.js

Show Email Instead of Username

git blame -e index.html

Output includes the email address of each author instead of (or in addition to) the name.

Blame at a Specific Commit or Branch

To see who was responsible for each line as of a specific commit or branch (not the current state):

# Blame as of a specific commit
git blame a3f92b1 index.html

# Blame as of a specific branch
git blame feature-login -- index.html

Compact Output — Show Short Hashes

git blame --abbrev=7 index.html

Limits the displayed hash to 7 characters (the default is already short, but this makes it consistent).

Ignore Whitespace Changes

If a line was only reformatted (whitespace changed) but not truly modified, that reformatting commit can be ignored to find the original author:

git blame -w index.html

Detect Moved or Copied Lines

Lines that were copied or moved from elsewhere in the same file can be traced to their original author:

# Detect lines moved/copied within the same file
git blame -M index.html

# Detect lines moved/copied from any other file in the commit
git blame -C index.html

Practical Scenario — Finding the Source of a Bug

# Step 1: A bug is on line 47 of app.js — find who last changed it
git blame -L 47,47 app.js
# Output:
# d4e5f6g7 (Suresh Nair  2023-10-10 16:45:00  47) user.login(null);

# Step 2: The commit d4e5f6g7 introduced this null argument — investigate it
git show d4e5f6g7

# Step 3: See the full context of that commit
git log --oneline -1 d4e5f6g7
# d4e5f6g Refactor user authentication module

git blame in Code Editors

Many code editors show Git blame information inline, so opening the terminal is not always needed:

  • VS Code — GitLens extension shows blame info next to each line
  • IntelliJ / WebStorm — Built-in Git annotate feature (right-click the gutter)
  • GitHub Web — Click "Blame" button on any file page to see blame in the browser

Git Blame on GitHub

On any file in a GitHub repository:

  1. Open the file
  2. Click the "Blame" button (next to "Raw" and "History")
  3. Each line shows the commit, author, and date in the left margin
  4. Click any commit hash to see the full commit details

Common Use Cases

SituationWhat git blame helps with
Found a bug on line 72Find out who wrote it and in which commit to understand the context
Unfamiliar piece of codeFind the original author to ask for clarification
Understanding why a decision was madeTrace the commit and read its message or linked issue
Code reviewCheck when and why certain sections were last changed

Summary

git blame shows the author, timestamp, and commit hash for every line in a file. It is an essential tool for code archaeology — understanding the history behind specific lines of code. Use -L to target specific line ranges, -w to ignore whitespace, and -M/-C to track moved lines. Most modern editors and GitHub itself offer built-in blame views that make this even easier to use.

Leave a Comment

Your email address will not be published. Required fields are marked *