Table of Contents
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>
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>
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,
Clone it on the CLI and
cd into it.
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.
mamboStores and inspect the
answer.cpp file's changes.
cd mamboStores git blame answer.cpp
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
git log --oneline
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.
Lastly, we can see that he modified the line:
1) #include <iostream>.
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
git blame -C answer.cpp
To check the lines copied or moved within the same file, including the file's original author, use the
git blame -M answer.cpp
To check the author's email address, instead of the username, use the
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.
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.
After creating the tag, we can git blame the modified line using the tag.
git blame v1.0 answer.cpp
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
Tell git the number of lines it should count from the first line.
git blame answer.cpp -L 1,+5
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
git blame --since=1,week answer.cpp
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 a forked Vue.js repository.
Clone the repository and
cd into it.
Then open it with Visual Code Studio. And install the two extensions.
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.
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.
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.
Click on View
And you land on the diff file on GitHub.
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.