Git blame Command
In this page:
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
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
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
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
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
git blame -C file.txt
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: