DEV Community

Srebalaji Thirumalai
Srebalaji Thirumalai

Posted on • Originally published at gitbetter.substack.com on

How to use git blame effectively

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Git blame is used to see the last commit and the last author who has modified for each line in the file.

In other words, using git blame, you can see the entire git revision of the file with respect to each line.

Git blame will come in handy when you want to which commit is responsible for the changes and which author has committed those changes.

Usage

git blame <file_name>

In the above example, I have blamed for a file. You can see the short commit hash, author name, and timestamp for each line throughout the file.

Blaming only a limited line range

Sometimes, You don’t need to blame the entire file, you just need to blame in a limited line range. Git blame provides the option for that.

-L will take the option for the start line and for the end line.

git blame <file_name> -L 80,100

The above command will blame from line 80 through line 100.

In the above example, see the starting line and the ending line.

Blaming by commit timestamp

You can also blame by commit timestamp

git blame <file_name> --since=3.days

This command will blame commits that are more than 3 days old.

Blame in Github and in Gitlab

There is an option to blame in Github and in Gitlab.

If you click on any file in Github you can see the blame option.

Using that option, you can see the entire git revision of that file.

Below is an example of blame in Github.

Git blame is a very effective tool that helps you to learn the entire git history of the file line by line.

And this also helps you to figure out which of your peers made that breaking change in the production so that you can blame them :P Just kidding don’t do that :)

Git blame is mostly used with some GUI tools. Because git blame in raw mode is not very readable.

There are also extensions in code editors like Sublime, VS Code you can configure.

Sublime - You can try Git blame extension

VS Code - You can try Git lens

Apart from these extensions, there is also another nicer way of viewing the blame. You can use Git GUI tool. It comes pre-installed with Git and it works with popular platforms.

git gui blame <file_name>

Apart from blaming, Git GUI also has other options. You can explore them too.

Hope you learned something :)

Thank you for reading :) :)

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Top comments (0)