This article covers how to use the command 'git blame'.
Despite its negative-sounding name, git blame can be very useful for identifying which developer changed each line in a file.
You can also use this command to find related changes made in the same commit.
To see the list of users who created/modified a file, you can use git blame along with the file name:
git blame <file-name>
git blame src/main.js
Blame by commit
By default, git blame will only show the last person who modified a line.
If you want to find out who modified a line before a certain point, you can specify a commit ID:
git blame <file-name> <commit-ID>
git blame src/main.js 2c86011d
Specify a line range
Next to each line in the output is the line number.
To get the blame history on part of a file, you can specify the line number range along with the -L parameter:
git blame -L <line-number-range> <file-name>
For example, to return lines 1 to 5:
git blame -L 1,5 src/main.js
Filter out whitespace changes
A lot of changes could just be whitespace changes. To filter these out, you can use the -w parameter:
git blame -w <file-name>
Filter out moved lines
Often, a developer will move a line of code without changing any of the logic.
To filter out moved lines from within the same file, you can use the -M parameter:
git blame -M <file-name>
Similarly, to filter out moved lines from another file, you can also use the -C parameter:
git blame -C <file-name>
Get the email
When using git blame, the output will provide the name of the users who made each line change.
If you need their email instead, you can use the -e parameter:
git blame -e <file-name>