0
0
Gitdevops~5 mins

git blame for line-by-line history - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to know who last changed each line in a file and when. Git blame helps you find that out by showing the author and commit for every line in a file.
When you want to find out who wrote or last changed a specific line in a file.
When you need to understand why a particular line of code was added or modified.
When you are reviewing code and want to ask the right person about a change.
When debugging and you want to trace the history of a line that causes a problem.
When preparing documentation or release notes and want to credit contributors.
Commands
This command shows who last changed each line in the file example.txt, along with the commit hash and timestamp.
Terminal
git blame example.txt
Expected OutputExpected
a1b2c3d4 (Alice 2024-06-01 10:15:30 +0000 1) First line of the file b5e6f7g8 (Bob 2024-06-02 14:20:10 +0000 2) Second line of the file c9d0e1f2 (Alice 2024-06-03 09:05:00 +0000 3) Third line of the file
This command limits the blame output to lines 2 through 3 in example.txt, so you only see the history for those lines.
Terminal
git blame -L 2,3 example.txt
Expected OutputExpected
b5e6f7g8 (Bob 2024-06-02 14:20:10 +0000 2) Second line of the file c9d0e1f2 (Alice 2024-06-03 09:05:00 +0000 3) Third line of the file
-L 2,3 - Limits output to lines 2 through 3
This command shows the blame output in a detailed, machine-readable format with extra metadata for each line.
Terminal
git blame -p example.txt
Expected OutputExpected
a1b2c3d4 1 1 1 author Alice author-mail <alice@example.com> author-time 1685600130 author-tz +0000 committer Alice committer-mail <alice@example.com> committer-time 1685600130 committer-tz +0000 summary Initial commit filename example.txt b5e6f7g8 2 2 1 author Bob author-mail <bob@example.com> author-time 1685690410 author-tz +0000 committer Bob committer-mail <bob@example.com> committer-time 1685690410 committer-tz +0000 summary Added second line filename example.txt c9d0e1f2 3 3 1 author Alice author-mail <alice@example.com> author-time 1685778300 author-tz +0000 committer Alice committer-mail <alice@example.com> committer-time 1685778300 committer-tz +0000 summary Added third line filename example.txt
-p - Shows detailed, porcelain format output
Key Concept

If you remember nothing else from git blame, remember: it shows who last changed each line in a file and when.

Common Mistakes
Running git blame on a file that does not exist or is not tracked by git.
Git blame needs a tracked file to show history; otherwise, it will error out.
Make sure the file exists and is committed in the git repository before running git blame.
Not using the -L flag when wanting to see blame for only specific lines.
Without -L, git blame shows the entire file, which can be overwhelming for large files.
Use git blame -L start,end filename to focus on the lines you care about.
Confusing git blame output with current file content only.
Git blame shows the last commit that changed each line, not just the current content, so lines may come from different commits.
Read the commit hash and author info to understand the history behind each line.
Summary
Use git blame filename to see who last changed each line in a file.
Use git blame -L start,end filename to limit output to specific lines.
Use git blame -p filename for detailed metadata about each line's history.