How to See Who Changed a Line in Git Using git blame
Use the
git blame command followed by the file name to see who last changed each line in that file. This command shows the author, commit hash, and timestamp for every line, helping you identify who changed a specific line.Syntax
The basic syntax of git blame is:
git blame [options] <file>: Shows the last change for each line in the specified file.- Options can filter or format the output, like
-Lto limit lines or-eto show email addresses.
bash
git blame [options] <file>
Example
This example shows how to find who last changed each line in a file named app.js. It helps you identify the author and commit for every line.
bash
git blame app.js
Output
e3a1b2c4 (Alice 2024-06-01 14:22:10 +0200 1) const greet = () => {
5f7d9e8a (Bob 2024-06-02 09:15:30 +0200 2) console.log('Hello, world!');
5f7d9e8a (Bob 2024-06-02 09:15:30 +0200 3) }
Common Pitfalls
Some common mistakes when using git blame include:
- Running
git blameon a file that was recently renamed or moved without using the--followoption, which can miss history. - Not limiting the output when the file is large, making it hard to find the line you want.
- Confusing
git blameoutput with the current author if the line was changed multiple times.
To avoid these, use git blame --follow <file> to track history across renames and git blame -L <start,end> <file> to focus on specific lines.
bash
git blame --follow app.js git blame -L 10,20 app.js
Quick Reference
| Command | Description |
|---|---|
| git blame | Show last change info for each line in the file |
| git blame --follow | Include history across file renames |
| git blame -L | Show blame for specific line range |
| git blame -e | Show author email instead of name |
| git blame -C | Detect lines copied or moved within the file |
Key Takeaways
Use
git blame <file> to see who last changed each line in a file.Add
--follow to track changes through file renames.Use
-L option to limit output to specific lines.Read the output carefully to understand author, commit, and timestamp per line.
Avoid confusion by combining blame with other git commands for full history.