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.
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.
That’s it for today :)
Hope you learned something.
Before signing up, I’d like to know something. Will you be interested in learning more about how people start successful open source projects, how they grow, how they collaborate, and what it is to be a contributor to open-source projects?
Reply to this email :)
See you next week :) :)