← Back to Git Course | Chapter 6: Advanced Commands | Lesson 5 of 7

Git blame Command

git blame is like asking 'who wrote this line?' for every line in a file. It shows the person and the day it was last changed.
Syntax
bash
git blame file_name
git blame -L start,end file_name

Viewing File Line Authorship

git blame <file> annotates every line of a file with the commit hash, author, and date that last changed it, which makes it the fastest way to answer "who wrote this and why" without digging through the full commit log by hand.

Example: Viewing File Line Authorship

bash
git blame file.txt

Checking Specific Line Ranges

Blaming a large file dumps annotations for every single line, which is unhelpful when you only care about a few. git blame -L 20,40 <file> restricts the output to lines 20 through 40, letting you focus on just the function or block you're actually investigating.

Example: Checking Specific Line Ranges

bash
git blame -L 20,40 file.txt

Showing Commit Timestamps

By default the date column follows your system locale, which can be inconsistent across machines. --date=short gives a plain YYYY-MM-DD format and --date=raw gives the raw Unix timestamp, both useful when you need output that's easy to parse or compare consistently.

Example: Showing Commit Timestamps

bash
git blame --date=short file.txt
git blame --date=raw file.txt

Ignoring White Spaces

A single reformatting commit (like an indentation change) can bury the real authorship history under one commit that touched every line. git blame -w ignores whitespace-only differences when attributing lines, so blame skips past the reformat and finds the commit that actually changed the logic.

Example: Ignoring White Spaces

bash
git blame -w file.txt

Tracking Copied Code

When code was moved or copied from elsewhere in the same commit history, plain blame would just credit whoever did the copy-paste. git blame -C (or -CCC for a more thorough search) follows that code's origin further back, crediting the commit that originally wrote it instead.

Example: Tracking Copied Code

bash
git blame -C file.txt
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming git blame shows who first wrote a line, when it shows who last changed it.
  2. Using it to blame a person, when it is meant to find the commit and its message that explain why.
  3. Running it on a huge file without -L, printing hundreds of lines when git blame -L 20,40 file.txt limits it.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.