Open In App

How to Use git-blame?

Last Updated : 23 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Git is a powerful tool for version control, and one of its most useful commands is git-blame. This command allows you to track changes in a file line by line, identifying who made each change and when it was made. This can be invaluable for understanding the history of a file, debugging issues, and improving collaboration within a team. In this article, we will explore how to use git-blame effectively.

What is git-blame?

git-blame is a Git command that displays each line of a file with information about the commit that last modified it. This includes the author of the change, the commit hash, and the timestamp of the change. This information can help you understand the context of changes and identify who to ask about specific lines of code.

Syntax:

git blame [options] <file-name>

Steps to Implement git-blame

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.

cloning create-react-app repository from GitHub

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:

The output of the git blame command on the CONTRIBUTING.md file

Tip: Observe that the last column of output displays the line number along with the code on that line.

Output study

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:

raw timestamp using -t option with the git blame command

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:

The output of the git blame command for a given range of lines

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:

The output of the git blame command with commits not older than three months (without removing unrequired commits)

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:

The output of the git blame command with commits not older than three months (after removing unrequired commits)

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.

Example: GitHub

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 :

labeled picture of blame option on GitHub

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.



Similar Reads

Difference Between Git remote prune, Git prune and Git fetch --prune
One of the great things about Git is that it's *very careful about deleting data*. This makes it pretty hard to lose commits or other valuable data in Git! A small downside of this is that you might sometimes see stale data that you actually don't need anymore. One of the best examples of this is references to remote branches that have already been
2 min read
Difference between “git add -A” and “git add”
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It is basically, software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Featu
3 min read
Git - Difference Between Git Revert, Checkout and Reset
While Working with Git in certain situations we want to undo changes in the working area or index area, sometimes remove commits locally or remotely and we need to reverse those changes. There are 3 different ways in which we can undo the changes in our repository, these are git reset, git checkout, and git revert. git checkout and git reset in fac
7 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
4 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
Git - git prune
In version control systems, Git has powerful features and flexibility. However, as projects evolve and repositories grow, managing Git's internal database becomes important. Enter git prune, a command designed to help you keep your repository lean and clean by removing unreachable or dangling objects. In this article, we'll explore the git prune co
5 min read