Git blame - How to find out who modified a line with Git

Johnny Simpson - Aug 30 '22 - - Dev Community

Have you ever found a bug inducing line change in your code, and wondered who made the change to that line? Fortunately, git has a command for that! git blame lets you pick a file and show who last changed every line. If you were wondering, it's called git blame because it lets you assign blame for the code breaking line you're investigating.

To find out who changed a line, simply run the following command where myfile.txt is the file you want to check:

git blame myfile.txt
Enter fullscreen mode Exit fullscreen mode

If you're interested in the format of git blame, it breaks down like this:

^665221a (Johnny Simpson 2022-04-30 20:58:04 +0100  10) import { v4 as uuid } from 'uuid';
^          ^                                         ^
|          |                                         |
Commit ID  |                                     Line number
           |
         Author and timestamp
Enter fullscreen mode Exit fullscreen mode

Using git blame on a particular line

This is really useful, but what if you want to hone in on a specific line, you can use the -L option. For example, if you want to see the change history between line 1 and 5, you would do the following:

git blame index.js -L 1,5
Enter fullscreen mode Exit fullscreen mode

Or, if you wanted to find the change history between lines 20 and 40, you could do the following:

git blame index.js -L 20,40
Enter fullscreen mode Exit fullscreen mode

Other useful git blame options

There are also a bunch of other useful git blame options that you might want to use. Here are some of the ones I use the most, and what they do.

Showing an author's email with git blame

All you have to to show the email address of an author only, is use the -e option:

git blame index.js -e
Enter fullscreen mode Exit fullscreen mode

Producing an output like this:

^665221a (<some@email.com> 2022-04-30 20:58:04 +0100   8) import { fileURLToPath } from 'url'
Enter fullscreen mode Exit fullscreen mode

Ignoring whitepsace with git blame

You can also ignore whitespace with git blame, should your code contain a lot of it. To do this, you can use the -w option:

git blame index.js -w
Enter fullscreen mode Exit fullscreen mode

Formatting lines with color using git blame

By default, git blame produces a wall of white or black text. If you want to differentiate different commits and changes by color, you can use the --color-lines or --color-by-age:

  • --color-lines colors lines if the line before was a different commit.
  • --color-by-age colors lines by their age.

Showing file names with git blame

To show the filename with git blame, use the -f option. This will show the file name along with the commit ID.

git blame index.js -f
Enter fullscreen mode Exit fullscreen mode

Will produce an output like this:

^665221a index.js (Johnny Simpson 2022-04-30 20:58:04 +0100  16) import dotenv from 'dotenv'
Enter fullscreen mode Exit fullscreen mode

Showing line changes from the bottom up (reversed) with git blame

You can also show line changes in reverse with git blame, meaning starting at the bottom, and going up. Just add the --reverse option to your command:

git blame index.js --reverse
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .