Git Blame: Track Who Changed Every Line
Use git blame to see who last modified each line of a file, when it changed, and in which commit. Essential for understanding code history.
git blame -L 10,20 src/app.tsxDetailed Explanation
What Does git blame Do?
git blame annotates each line of a file with the commit hash, author, and date of the last change to that line. It is an investigative tool for understanding the history behind specific code.
Basic Usage
# Blame an entire file
git blame src/app.tsx
# Blame specific line range
git blame -L 10,20 src/app.tsx
# Blame starting from a specific function (regex)
git blame -L '/function App/,/^}/' src/app.tsx
Output Format
a1b2c3d4 (Jane Doe 2025-03-15 14:22:01 +0000 10) function App() {
f4e5d6a7 (John Smith 2025-04-02 09:11:33 +0000 11) const [count, setCount] = useState(0);
a1b2c3d4 (Jane Doe 2025-03-15 14:22:01 +0000 12) return <div>{count}</div>;
Ignoring Whitespace and Formatting Changes
Large reformatting commits can obscure meaningful blame data. Use -w to ignore whitespace:
git blame -w src/app.tsx
For projects that have had major formatting commits (e.g., running Prettier for the first time), create a .git-blame-ignore-revs file:
# .git-blame-ignore-revs
# Prettier reformatting
a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
git config blame.ignoreRevsFile .git-blame-ignore-revs
Going Deeper
To see the full commit that changed a line:
git blame src/app.tsx
# Note the hash, then:
git show a1b2c3d4
Tips
Git blame is not about assigning fault — it is about understanding context. When you find a confusing line of code, blame reveals the commit message and PR that introduced it, giving you the "why" behind the code. Combine it with git log -p for a complete picture.
Use Case
A developer encounters a confusing piece of logic in a shared module and uses git blame to find the original commit and its associated pull request for context.