Git Blame

Updated · 11 min read
mediumGit 2.53gitgit-blameignore-revshistoryinterview

git blame is a command that shows, for every line of a file, the commit that last changed it, the author of that commit and the date. It is used to answer the question "why is this line like this?": once you know the commit, git show gives you its message and the rest of the change. Despite its name, it is not about blaming people. It is about finding the reason behind a line.

It is like the "Track changes" view in a shared document, where every sentence is marked with who wrote it and when. You don't use it to point fingers. You use it to know whom to ask, and which discussion to read.

Syntax

Terminal
git blame [options] [<commit>] [--] <file>
OptionDescription
-L <start>,<end>It is used to show only a range of lines.
-L :<name>It is used to show only the function or method called <name>.
-wIt is used to ignore changes that only added or removed whitespace.
--ignore-rev <commit>It is used to skip a commit, such as a reformatting commit, and look past it.
--ignore-revs-file <file>It is used to skip every commit listed in a file.
-CIt is used to detect lines moved or copied from other files in the same commit.
-MIt is used to detect lines moved within the file.
-eIt is used to show email addresses instead of names.
-sIt is used to hide the author and date, showing only commit IDs.
<commit>It is used to blame the file as it was in an older commit.

The Starting Point

The Fee Tracker's FeeCalculator.java was written by three people over several commits:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
    int total(int terms) {
        return terms * 12000;
    }
}
EOF
git add FeeCalculator.java
git commit --quiet -m "Add the fee calculator"
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
    int total(int terms) {
        return terms * 12500;
    }
 
    int lateFee(int daysLate) {
        return daysLate > 0 ? 250 : 0;
    }
}
EOF
git commit --quiet -a --author="Srihitha Anamaneni <srihitha@example.com>" -m "Raise the term fee and add the late fee"
sed -i.bak "s/? 250 : 0/? 300 : 0/" FeeCalculator.java
rm FeeCalculator.java.bak
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m 'Raise the late fee to $300 (office request)'
git log --oneline
Output
05c2869 (HEAD -> main) Raise the late fee to $300 (office request)
cf777ed Raise the term fee and add the late fee
e538a57 Add the fee calculator

Example 1: Blame a File

Terminal
git blame FeeCalculator.java
Output
^e538a57 (Navika Anamaneni   2026-01-05 10:06:00 -0600 1) class FeeCalculator {
^e538a57 (Navika Anamaneni   2026-01-05 10:06:00 -0600 2)     int total(int terms) {
cf777ed3 (Srihitha Anamaneni 2026-01-05 10:08:00 -0600 3)         return terms * 12500;
cf777ed3 (Srihitha Anamaneni 2026-01-05 10:08:00 -0600 4)     }
cf777ed3 (Srihitha Anamaneni 2026-01-05 10:08:00 -0600 5)
cf777ed3 (Srihitha Anamaneni 2026-01-05 10:08:00 -0600 6)     int lateFee(int daysLate) {
05c28690 (Advitha Anamaneni  2026-01-05 10:11:00 -0600 7)         return daysLate > 0 ? 300 : 0;
^e538a57 (Navika Anamaneni   2026-01-05 10:06:00 -0600 8)     }
^e538a57 (Navika Anamaneni   2026-01-05 10:06:00 -0600 9) }

Explanation: each line starts with the short ID of the commit that last changed it, then the author, the date and time, and the line number. The first commit's lines start with ^, meaning "this is the first commit, the line has been there since the beginning". Reading it, Navika sees that the term fee line came from Srihitha, and the late fee amount from Advitha.

To learn why, she looks at the commit:

Terminal
git show --stat $(git blame -L 7,7 --porcelain FeeCalculator.java | head -1 | cut -d " " -f 1)
Output
commit 05c286905f889f4172eff780f9836f2f72fb2cd4 (HEAD -> main)
Author: Advitha Anamaneni <advitha@example.com>
Date:   Mon Jan 5 10:11:00 2026 -0600
 
    Raise the late fee to $300 (office request)
 
 FeeCalculator.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--porcelain prints blame in a format meant for scripts, whose first word is the full commit ID. Usually you simply copy the short ID from the blame output: git show 1a2b3c4. The message, "office request", is the answer.

Reading one line of git blame

Example 2: Only Some Lines, or One Method

Terminal
git blame -L 6,8 FeeCalculator.java
Output
cf777ed3 (Srihitha Anamaneni 2026-01-05 10:08:00 -0600 6)     int lateFee(int daysLate) {
05c28690 (Advitha Anamaneni  2026-01-05 10:11:00 -0600 7)         return daysLate > 0 ? 300 : 0;
^e538a57 (Navika Anamaneni   2026-01-05 10:06:00 -0600 8)     }

-L 6,8 shows lines 6 to 8 only. -L :lateFee shows the method whose header matches lateFee, wherever it is in the file. For Java, it finds methods correctly after you add *.java diff=java to .gitattributes (see "How to Read a Diff"):

Terminal
echo "*.java diff=java" > .gitattributes
git blame -s -L :lateFee FeeCalculator.java
Output
cf777ed3 6)     int lateFee(int daysLate) {
05c28690 7)         return daysLate > 0 ? 300 : 0;
^e538a57 8)     }
^e538a57 9) }

-s leaves out the author and date, which is handy when you only need the commits. The range runs from the method's header until the next method begins, so here it also includes the class's closing brace.

Example 3: Look Past Whitespace and Reformatting

Navika reformats the file, changing the indentation from four spaces to two. Now every changed line is "hers":

Terminal
git add .gitattributes
git commit --quiet -m "Use Java hunk headers"
sed -i.bak "s/^    /  /; s/^      /    /" FeeCalculator.java
rm FeeCalculator.java.bak
git commit --quiet -am "Reformat with two-space indentation"
git blame -s FeeCalculator.java
Output
^e538a57 1) class FeeCalculator {
d91c4c4e 2)   int total(int terms) {
d91c4c4e 3)     return terms * 12500;
d91c4c4e 4)   }
cf777ed3 5)
d91c4c4e 6)   int lateFee(int daysLate) {
d91c4c4e 7)     return daysLate > 0 ? 300 : 0;
d91c4c4e 8)   }
^e538a57 9) }

The real authors are hidden behind the reformatting commit. Two ways to see past it:

-w ignores whitespace-only changes:

Terminal
git blame -s -w FeeCalculator.java
Output
^e538a57 1) class FeeCalculator {
^e538a57 2)   int total(int terms) {
cf777ed3 3)     return terms * 12500;
cf777ed3 4)   }
cf777ed3 5)
cf777ed3 6)   int lateFee(int daysLate) {
05c28690 7)     return daysLate > 0 ? 300 : 0;
^e538a57 8)   }
^e538a57 9) }

An ignore-revs file lists commits that blame should always skip, such as bulk reformatting. Many projects keep one called .git-blame-ignore-revs, and tell Git about it once:

Terminal
git rev-parse HEAD > .git-blame-ignore-revs
git config blame.ignoreRevsFile .git-blame-ignore-revs
git blame -s FeeCalculator.java
Output
^e538a57 1) class FeeCalculator {
^e538a57 2)   int total(int terms) {
cf777ed3 3)     return terms * 12500;
cf777ed3 4)   }
cf777ed3 5)
cf777ed3 6)   int lateFee(int daysLate) {
05c28690 7)     return daysLate > 0 ? 300 : 0;
^e538a57 8)   }
^e538a57 9) }

Explanation: with the reformatting commit listed in the file, blame looks past it and shows the commits that really changed each line. Committing .git-blame-ignore-revs lets the whole team use it (GitHub's blame view also reads a file with exactly that name).

Example 4: Find Moved Code with -C

Srihitha moves the lateFee method to its own class, LateFee.java. A plain blame of the new file credits her for every line:

Terminal
cat > LateFee.java <<'EOF'
class LateFee {
  int lateFee(int daysLate) {
    return daysLate > 0 ? 300 : 0;
  }
}
EOF
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
  int total(int terms) {
    return terms * 12500;
  }
}
EOF
git add .
git commit --quiet --author="Srihitha Anamaneni <srihitha@example.com>" -m "Move the late fee to its own class"
git blame -s -C LateFee.java
Output
a29f0cfc LateFee.java       1) class LateFee {
cf777ed3 FeeCalculator.java 2)   int lateFee(int daysLate) {
05c28690 FeeCalculator.java 3)     return daysLate > 0 ? 300 : 0;
^e538a57 FeeCalculator.java 4)   }
^e538a57 FeeCalculator.java 5) }

Explanation: -C noticed that the method's lines came from FeeCalculator.java in the same commit, and followed them there. The line with the amount is credited to Advitha's commit again, and the file name column shows where each line originally lived. Without -C, all lines would show the move commit.

Common Mistakes

Treating blame as a verdict on a person. The last person to touch a line often only moved or reformatted it. Read the commit.

Forgetting -w or an ignore-revs file after a reformat. Every line then points to the reformatting commit.

Blaming the current file when the bug is older. Pass a commit to see a file as it was: git blame v1.0.0 -- FeeCalculator.java.

Expecting blame to show deleted lines. It only covers lines that exist. To find when a line was removed, use git log -S (see "Searching History").

Reading only the summary. The short line in blame is one commit. git show <commit> shows the whole change and its message.

Under the Hood

Blame works backward. It starts with the current file, and for each line, looks at the newest commit that changed the file: if the commit's diff added that line, the line is blamed on it. Lines the commit didn't add are passed on to its parent, and the search continues until every line is assigned. -M and -C additionally compare against other parts of the same file or other files in the commit, to recognize moved text instead of treating it as new. --ignore-rev tells blame to treat a commit as if its changes had been made by its parent, passing those lines further back.

Interview Corner

What does git blame do?

It annotates each line of a file with the commit that last changed it, the author and the date. It is used to find the commit, and therefore the reason, behind a line.

How do you ignore whitespace or formatting commits in blame?

Use -w to ignore whitespace-only changes, or --ignore-rev <commit>, or list such commits in a file (often .git-blame-ignore-revs) and set blame.ignoreRevsFile.

How do you blame only part of a file?

git blame -L <start>,<end> <file> for a line range, or -L :<function> <file> for a function or method.

How can blame follow code that was moved from another file?

git blame -C <file> detects lines moved or copied from other files in the same commit. Repeating it (-C -C) also looks at other commits.

Practice

Challenge: who chose 12500? Using blame on FeeCalculator.java, find which commit set the term fee to 12500, and print its author and message. Use the ignore-revs setting from above.

What does Git print? Before you open the solution, decide who the author is, and whether the move commit or the reformat commit gets in the way.

Show solution
Terminal
git blame -s -L 3,3 FeeCalculator.java
git log -1 --format="%an: %s" $(git blame -L 3,3 --porcelain FeeCalculator.java | head -1 | cut -d " " -f 1)
Output
cf777ed3 3)     return terms * 12500;
Srihitha Anamaneni: Raise the term fee and add the late fee

Srihitha set it, in "Raise the term fee and add the late fee". The reformat commit is skipped by the ignore-revs file, and the move commit didn't touch this line in FeeCalculator.java, so blame goes straight back to the commit that really changed it.

Checkpoint. In a project of your own, pick a line that puzzles you, find its commit with git blame, and read the full change with git show. Then reformat a file, commit it, and add the commit to .git-blame-ignore-revs.

Quick check: Git blame

4 questions
1What does git blame show for each line?
2Which option ignores whitespace-only changes?
3How do you make blame always skip a reformatting commit for the whole team?
4Which option follows lines moved from another file?

Key Takeaways

Quick reference

CommandIt is used to
git blame <file>Annotate every line
git blame -L 10,20 <file>Annotate lines 10 to 20
git blame -L :<method> <file>Annotate one method
git blame -w <file>Ignore whitespace changes
git config blame.ignoreRevsFile .git-blame-ignore-revsSkip listed commits
git blame -C <file>Follow moved or copied lines
git blame <commit> -- <file>Blame an older version

Was this tutorial helpful?

Lesson 66 of 106