git blame explained in layman's terms [Practical Examples]


GIT

Author: Steve Alila
Reviewer: Deepak Prasad

git blame command cheat sheet

Git blame is a command to inspect who made a change and when they made it. You can view the details on the terminal or a graphical user interface.

To see the changes on the CLI, run the command on a file.

git blame <file>

You can also specify the tag.

git blame <tag> <file>

OR

Reduce the number of viewable lines using the -L flag by specifying the target start or end positions.

git blame <file> -L <start line>,<end line>

#OR
​
git blame <file> -L <start line>,+<end line>
​

OR

Specify the target duration

git blame <file> --since=<start line>,<duration>

Lastly, you can view who made what change, when, and the modified remote using a GUI application. For instance, this tutorial uses Visual Studio Code with extensions: Git Blame and GitLens.

Now that you have a brief knowledge of what it takes to use git blame, let us set up a laboratory and practice using it.

 

Lab setup to explore git blame

I am copying the URL of a GitHub repository, cppApp.

git blame explained in layman's terms [Practical Examples]

Clone it on the CLI and cd into it.

cd cppApp

git blame explained in layman's terms [Practical Examples]

Let us do practice using the cloned repository.

 

Practice git blame using the CLI

Example-1: View the file's changes line by line

To view who made a change on a line and when they made it, use the blame command as follows:

First, check all files in the target folder.

ls mamboStores

git blame explained in layman's terms [Practical Examples]

cd into mamboStores and inspect the answer.cpp file's changes.

cd mamboStores

git blame answer.cpp

git blame command

The first part of the above result is the commit id. The second part is the modified file. The third part is the author (and date of changing the file), whereas the last part is the modified line. Here is a further illustration of what is happening.

To confirm the commit id, whose author we want to blame, run the log command with the --oneline option.

git log --oneline

git blame explained in layman's terms [Practical Examples]

The modified file is question1/answer.cpp. We blame Stevealila for changing the file on 25th January 2022 at 5:46 pm, Nairobi time: Stevealila 2022-01-25 17:46:14 +0300.

git blame explained in layman's terms [Practical Examples]

Lastly, we can see that he modified the line: 1) #include <iostream>.

git blame explained in layman's terms [Practical Examples]

 

Bonus tricks

From the above results, we see the author's username. What if he copied and pasted the line? Maybe we are only interested in his email address instead of the username. Here is how to check the details.

To check the lines copied or moved from another file and the file's original author, use the -C option.

git blame -C answer.cpp

To check the lines copied or moved within the same file, including the file's original author, use the -M option.

git blame -M answer.cpp

To check the author's email address, instead of the username, use the -e option

git blame -e answer.cpp

 

Example-2: Git blame using a tag

Create a git tag before seeing how to use them in the blame.

Log the history.

git log

Create an annotated tag.

git tag -a v1.0 -m "major release is ready"

Then relog the history. We see our newly created tag as follows.

git blame explained in layman's terms [Practical Examples]

After creating the tag, we can git blame the modified line using the tag.

git blame v1.0 answer.cpp

git blame explained in layman's terms [Practical Examples]

 

Example-3: Control the number of viewable lines using the -L option

Say we are only interested in the modification details of the first five lines of the answer.cpp file. We can view the details using the -L option, as follows.

Tell git the start and finish lines.

git blame answer.cpp -L 1,5

OR

Tell git the number of lines it should count from the first line.

git blame answer.cpp -L 1,+5

git blame explained in layman's terms [Practical Examples]

 

Example-3: View lines modified over a duration

Assume we want to check the changes made on the lines of the answer.cpp over the past one week. We can do that using the --since option.

git blame --since=1,week answer.cpp

git blame explained in layman's terms [Practical Examples]

That is all master before getting started git blame on the CLI. What if you want to see the changes on a GUI? That is where a code editor with its rich extensions comes in.

Let us see how to manage git blame on Visual Code Studio.

 

How to use git blame in Visual Studio Code

In this section, I am using my private forked Vue.js repository.

Clone the repository and cd into it.

git blame explained in layman's terms [Practical Examples]

Then open it with Visual Code Studio. And install the two extensions.

git blame explained in layman's terms [Practical Examples]

git blame explained in layman's terms [Practical Examples]

 

We are going to see who made changes to the index.js file. Navigate it using following the its path: Packages -> vue3-jest -> lib -> index.js.

On the top of the file, you see the last (as per our inspection date) person to modify the file, when they made the changes and the number of collaborators.

git blame on Visual Studio Code

Hover on any line and see its last modifier, the modification date, and the commit message. For instance, GitLens tells us that Edd Yerburgh modified line 1 three years ago as a feature commit, whereas Xin Du modified line 14 three years ago as a fixture commit.

git blame explained in layman's terms [Practical Examples]

Additionally, we can view the profile photo and more author details by moving the cursor onto their name.

Looking at the bottommost part of Visual Studio, Git Blame tells us who to blame for the particular line's changes and when they made the change. Clicking on the information, a prompt with the commit details opens up, asking us to view the details on the remote.

git blame explained in layman's terms [Practical Examples]

Click on View

git blame explained in layman's terms [Practical Examples]

And you land on the diff file on GitHub.

git blame explained in layman's terms [Practical Examples]

 

Conclusion

You just learned the role of git blame: inspecting who made what change and when they did it. Apart from tracking the modifications on the CLI, you can now see the changes on Visual Studio Code's GUI using GitLens or Git Blame extensions.

Now is the time to simplify repository management by taking an in-depth look at the file line modifications, as you saw in this tutorial.

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment