How to Use git-blame?
Git provides us with various methods to inspect a git repository. We can get complete detail of commit history, how the initial code evolved after modification, the details of modification including the name of the author, time of modification, modified code. Some of the commands that git provide us to inspect and track a git repository are as follows:
- git status: It shows the current state of the git repository. It tells information about files if they are tracked, untracked, staged or unstaged, etc.
- git log: It tells detail about the commit history.
- git tag: It helps in creating checkpoints in a git commit history at specific commits which can be viewed later using the git show command.
Also, there is another powerful git command to inspect a git repository, the git blame command.
- What is Git Blame
- Syntax and Usage
- Command Line And GUI Based Approach
The git blame command displays the details of the author who last modified or added each line of code in a given file along with the commit id of modification. The commit id can be used with the git log command to get details of the commit. It will be demonstrated later in this article.
We can identify the name and email id of the last person who made changes or added a given line of code in a file using the git blame command. It would help us to reach that person and enquire him about that piece of code and its functionality on certain occasions.
git blame [options] <file-name>
Let’s say in a git repository we have to identify the author details and commit information of a given line of code in a given file of the repository. The same is shown below. The command is illustrated below with help of an example
Step 1: Move to an empty folder and initialize it with an empty git repository.
Step 2: Clone the repository for which you want the information. Here I am cloning a famous repository create-react-app from GitHub. Make sure to change the directory to the cloned repository.
Step 3: To get the author name and commit information, execute the git blame command as shown below. Replace the <file-name> option with the relative path of the file from the current directory for which you want details:
git blame <file-name>
Step 4: Here I want details for the file CONTRIBUTING.md which is present in the same directory I am currently at. So, I will execute the below command:
git blame CONTRIBUTING.md
Output: It looks like below as follows:
Tip: Observe that the last column of output displays the line number along with the code on that line.
Let’s examine the first line of output:
|Commit Id||Author Name||Timestamp||Code|
|46cf3fc43||Dan Abramov||2016-09-02 14:29:08 +0100||# Contributing to Create React App|
- Commit Id: It is the commit Id of the last commit in which this line was modified. In upcoming lines, we will use it to get the details of the commit.
- Author Name: It is the name of the author who last modified the given line of code. We will see how to get the author’s email id instead of the author’s name.
- Timestamp: It is the date and time when the commit was made expressed in standard notation. We can get the raw timestamp using the -t option as shown below:
git blame -t CONTRIBUTING.md
Output is shown below:
Viewing the commit details: In order to view the commit details, we will execute the git log command with the commit id for which we want the details. So, copy the commit id from the previous output of the git blame command. In my case, I want the commit details for the first line of the CONTRIBUTING.md file, so I will copy the commit id from the previous output of the git blame command. The commit id for that is 46cf3fc43.
Now, execute the below command. Replace <commit-id> with the commit id that you copied just now.
git log -p <commit-id>
In my case the command is below:
git log -p 46cf3fc43
The git log command helps to view the commit history. The -p or –patch option shows the author details and changes (addition or deletion) in the code in the commit of commit id provided.
Options with git blame: Git provides various options with the git blame command for viewing the details of modification in code in a file. Some commonly used options are given below:
To view output for a range of lines
git blame -L start-line,end-line <file-name>
The below command shows output for lines 2 to 5 both inclusive.
git blame -L 2,5 CONTRIBUTING.md
The output is shown below:
Note: You will get the same output for command – git blame -L 4,+3 README.md. Here 4,+3 signifies output for 3 lines starting from the fourth line.
To view the author email id
git blame -e CONTRIBUTING.md
The above command will show the author’s email id instead of the author’s name in the result. The output is shown below:
To view changes not older than the given time
git blame --since=3.months-- CONTRIBUTING.md
The above command is used to view git blame output for commits not older than 3 months for the file CONTRIBUTING.md. The output is shown below:
In the above command, we can see that it shows several commits with commit id starting with ^. The caret symbols signify the commits made before the time specified, in our case, it is three months. Hence we need to filter out them from our output. To tackle that we can modify our code as below:
git blame --since=3.months -- CONTRIBUTING.md | grep -v '^\^'
The above code simply filters the output after removing commits starting with the ^ symbol. Hence, the output shows commits not older than three months (from the time the code is executed). The result is shown below:
An alternate option to the command line (GUI based): Git hosting sites like – GitHub, Bitbucket, GitLab offer GUI displays for git blame, which gives a clear display of modification history in a file.
Step 1: Navigate to the file for which you want to see the modification history and click on the Blame option as shown below.
Step 2: The structure of output is described in the image below :
Hence, we saw that git blame is a very useful command in inspecting a repository. It gives a great insight into the commit history and can be used along with other git commands to get desired results.