Who Changed This Line? git blame and Ignoring Noise Commits
Find the commit behind every line with git blame: reading its output, limiting it to line ranges, functions and older revisions, ignoring whitespace and formatting commits with .git-blame-ignore-revs, following moved and copied code, and digging further with log -L and log -S.
kvi5hnuCode Sprout team member7 min readOn this page
When a line of code puzzles you, the fastest route to an explanation is usually the commit that introduced it: its message, its pull request, the other changes made with it. git blame shows, for every line of a file, the last commit that changed it, with its author and date. Despite the name, it's an investigation tool, not a way to assign fault. Most editors have it built in as "annotate" or inline blame.
Reading blame output
git init -q trail-guide && cd trail-guide
cat > calc.js <<'EOF'
function total(values) {
return values.reduce((sum, v) => sum + v, 0);
}
EOF
git add .; git commit -q -m "Add total" --author="Ana Diaz <ana@example.com>"
sed -i 's/, 0);/, 0) || 0;/' calc.js
git commit -q -am "Guard against empty totals" --author="Ravi Kumar <ravi@example.com>"
git blame calc.js
echo "=== shorter: no author or date"
git blame -s calc.js
echo "=== and the commit behind line 2"
git show -s --format='%h %an: %s' "$(git blame -L 2,2 --porcelain calc.js | head -1 | cut -d' ' -f1)"
^dda2478 (Ana Diaz 2026-03-02 10:00:00 +0000 1) function total(values) {
1b6ed18a (Ravi Kumar 2026-03-02 10:00:00 +0000 2) return values.reduce((sum, v) => sum + v, 0) || 0;
^dda2478 (Ana Diaz 2026-03-02 10:00:00 +0000 3) }
=== shorter: no author or date
^dda2478 1) function total(values) {
1b6ed18a 2) return values.reduce((sum, v) => sum + v, 0) || 0;
^dda2478 3) }
=== and the commit behind line 2
1b6ed18 Ravi Kumar: Guard against empty totals
Each line shows:
- the commit ID that last changed the line. A leading
^marks a boundary commit: the line dates from the earliest commit blame looked at, here the root commit; - the author and date of that commit;
- the line number and the line itself.
Useful display options: -s hides author and date, -e shows emails, --date=short shortens dates, and -c uses an older, tab-separated layout. For scripts, --porcelain and --line-porcelain print machine-readable blocks with every field.
Narrowing it down
git blame -L 40,60 app.js # lines 40 to 60
git blame -L 40,+10 app.js # 10 lines from line 40
git blame -L '/function average/,+5' stats.js # from a matching line
git blame -L :average stats.js # the whole function named average
git blame v1.0 -- app.js # the file as it was in v1.0
git blame abc1234^ -- app.js # just before commit abc1234
The last form is how you dig deeper: blame says line 12 came from abc1234, but that commit only renamed a variable. Blame its parent, abc1234^, to see who wrote the line before that. Editors and hosting services offer the same as "blame prior to this change".
-L :name finds the function using the same rules as diff's hunk headers (article 7). For languages with a diff driver configured in .gitattributes, such as *.py diff=python, it recognizes that language's definitions (article 35).
Ignoring whitespace and formatting commits
A commit that reformats a file, re-indenting or switching quote styles, becomes the "last change" for every line, hiding the real history. Git has two remedies.
-w ignores whitespace changes when assigning lines. For a commit that only re-indented, blame looks past it:
git init -q trail-guide && cd trail-guide
printf 'function total(a, b) {\n return a + b;\n}\n' > calc.js
git add .; git commit -q -m "Add total" --author="Ana Diaz <ana@example.com>"
sed -i 's/a + b;/a + b + 0;/' calc.js
git commit -q -am "Make total numeric" --author="Ravi Kumar <ravi@example.com>"
sed -i 's/^ /\t/' calc.js; git commit -q -am "Reformat with tabs"
echo "=== plain blame: the reformat owns line 2"
git blame -s calc.js
echo "=== -w: whitespace ignored"
git blame -s -w calc.js
echo "=== --ignore-rev: skip the formatting commit"
git blame -s --ignore-rev HEAD calc.js
=== plain blame: the reformat owns line 2
^4e1a113 1) function total(a, b) {
e27f8401 2) return a + b + 0;
^4e1a113 3) }
=== -w: whitespace ignored
^4e1a113 1) function total(a, b) {
70d930f3 2) return a + b + 0;
^4e1a113 3) }
=== --ignore-rev: skip the formatting commit
^4e1a113 1) function total(a, b) {
70d930f3 2) return a + b + 0;
^4e1a113 3) }
For formatting commits that change more than whitespace, such as running a code formatter across the project, list them in a file, conventionally named .git-blame-ignore-revs, with one full commit ID per line and # comments:
# Reformat with Prettier 3
4e1a1130c3b5a3f9d0b6e0e3bd2d1c8a5c1e2f3a
# Switch to tabs
38402c5120551dd40e7efbdefe00c34ad193c0ac
Then use it, once per clone:
git config blame.ignoreRevsFile .git-blame-ignore-revs
From then on, plain git blame skips those commits and assigns each line to the commit before. GitHub reads .git-blame-ignore-revs automatically in its blame view, and GitLab supports it too. Two optional settings show where it made a difference:
blame.markIgnoredLines trueprefixes lines that were re-assigned past an ignored commit with?;blame.markUnblamableLines truemarks with*lines that the ignored commit added, which have no earlier owner.
Moved and copied code
By default, blame follows a line only within the same file. When code moves between files, the commit that moved it gets the blame:
git init -q trail-guide && cd trail-guide
cat > calc.js <<'EOF'
function total(values) {
return values.reduce((sum, v) => sum + v, 0);
}
function average(values) {
const count = values.length;
return count === 0 ? 0 : total(values) / count;
}
EOF
git add .; git commit -q -m "Add calc" --author="Ana Diaz <ana@example.com>"
head -3 calc.js > tmp.js; tail -4 calc.js > stats.js; mv tmp.js calc.js
git add .; git commit -q -m "Move average to stats.js"
echo "=== plain: the move commit owns everything"
git blame -s stats.js
echo "=== -C: traced back to calc.js"
git blame -s -C stats.js
=== plain: the move commit owns everything
382b236 1) function average(values) {
382b236 2) const count = values.length;
382b236 3) return count === 0 ? 0 : total(values) / count;
382b236 4) }
=== -C: traced back to calc.js
^fc12215 calc.js 1) function average(values) {
^fc12215 calc.js 2) const count = values.length;
^fc12215 calc.js 3) return count === 0 ? 0 : total(values) / count;
^fc12215 calc.js 4) }
| Option | Detects lines that were... |
|---|---|
-M | moved within the same file |
-C | moved or copied from other files changed in the same commit |
-C -C | copied from other files in the commit that created the file too |
-C -C -C | copied from any file in any commit. Slow on large repositories |
Detection needs a block of at least about 20 letters and digits for -M, and 40 for -C, so very short lines may not be matched. A number after the option changes that threshold, as in -C10.
Beyond blame: log -L and log -S
Blame shows the last change to each line. To see every change to a range of lines, use git log -L:
git log -L 40,60:app.js # each commit that changed lines 40-60, with diffs
git log -L :average:stats.js --oneline # the history of one function
And blame can't show lines that no longer exist. To find when something was removed, or added, search the changes with the pickaxe options (article 9):
git log -S "legacyTotal" --oneline # commits where the count of "legacyTotal" changed
git log -G "total\(.*\)" --oneline # commits whose diff has a line matching the regex
-S finds the commits that added or deleted the string, which is ideal for "when did this function disappear?".
Using blame well
- Read the commit, not just the name.
git show <id>gives the message and full change, and the pull request linked from it usually explains why. - Expect mechanical commits. Merges with conflict resolutions, reformats and mass renames own many lines. Ignore them, or blame the parent.
- Write good messages. Blame is only as useful as the commit messages it leads to (article 6).
What's current
blame.ignoreRevsFilewith a committed.git-blame-ignore-revsfile is the standard way to hide formatting commits, and GitHub and GitLab honour it in their blame views.- Editors show blame inline: VS Code's built-in source control and extensions such as GitLens, the JetBrains IDEs' "Annotate with Git Blame", and similar features elsewhere.
- Hosting services' blame views offer "view blame prior to this change", the same as
git blame <commit>^ -- <file>.
Common questions
What does the ^ before a commit ID in blame output mean?
It's a boundary commit: blame couldn't look further back. Usually that's the repository's first commit, or the limit of a range you gave, as in git blame v1.0..HEAD -- file. In a shallow clone, it's where the history was cut.
Why does blame show the person who reformatted the file?
Their commit was the last to change those lines. Use -w for whitespace-only changes, or add the commit to .git-blame-ignore-revs and set blame.ignoreRevsFile.
How do I find who deleted a line?
Blame can't, because the line isn't in the file anymore. Use git log -S "<text from the line>" -- <file>, which finds the commits that added and removed it. Or git blame --reverse <old-commit>..HEAD -- <file>, which shows the last commit in which each old line still existed.
Does --ignore-revs-file need full commit IDs?
Yes, the file must contain full 40-character (or 64 for SHA-256) IDs. Abbreviated ones aren't accepted.
Summary
git blame <file>shows the last commit, author and date for each line.^marks boundary commits.-Llimits it to line ranges, regular expressions or a function (-L :name). Blame an older revision, or<commit>^, to dig past a change.-wignores whitespace,--ignore-revskips one commit, andblame.ignoreRevsFile .git-blame-ignore-revsskips formatting commits permanently.-Mand-Cfollow lines moved within and between files.git log -Lshows every change to a range or function, andgit log -Sfinds lines that were removed.
Practice questions
Your team ran a code formatter over the whole repository in one commit, and now blame shows that commit everywhere. How do you fix it for everyone?
Answer
Commit a .git-blame-ignore-revs file containing the formatter commit's full ID, with a comment saying what it was. Ask everyone to run git config blame.ignoreRevsFile .git-blame-ignore-revs, or add it to the project's setup instructions. GitHub and GitLab read the file automatically for their blame views.
Blame shows line 87 of orders.py was last changed by a commit that only renamed a variable. How do you find who wrote the line originally?
Answer
Blame the file as it was just before that commit: git blame <commit>^ -- orders.py, then look at the matching line, whose number may differ slightly. Repeat until you reach a meaningful change. Alternatively, git log -L 87,87:orders.py shows every commit that changed the line, with diffs.
A function called applyDiscount used to exist and is now gone. How do you find the commit that deleted it?
Answer
git log -S "applyDiscount" --oneline lists the commits where the number of occurrences of the string changed: the one that added it and the one that removed it, plus any in between. git show <id> on the most recent confirms it. Blame can't help, because the line no longer exists.
Previous: Naming Commits: Revision Syntax, Ancestry, Ranges and Reflog References
Next: Finding the Commit That Broke Something With git bisect

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.