Git

How to Use git blame

git blame is a very good tracking command for Git. git blame shows the author information of each line of the project’s last modified source file. You can find the author name, author email, the commit hash etc of the last modified source file line by line. You will see shortly what I mean practically.

In this article, I am going to show you how to use git blame to track the authors of a project. So, let’s get started.

Setting Up an Example Git Repository:


If you’re learning Git, then you can clone any Git repository from GitHub to your computer to try out the commands in this article. If you want to use your own Git repository, that’s fine as well.

I will clone the h5bp/html5-boilerplate Git repository from GitHub for the demonstration of git blame in this article.

$ git clone https://github.com/h5bp/html5-boilerplate.git

Now, navigate to the html5-boilerplate/ directory as follows:

$ cd html5-boilerplate/

In the html5-boilerplate/ directory, I have some files and directories. In the next section, I will show you how to use git blame on this Git repository.

Basic Usage:

To find the author and commit information of each line of the last modified version of the file (let’s say myfile) in your Git repository, you run git blame as follows:

$ git blame myfile

In my Git repository, I have a file gulpfile.babel.js. Let’s say, I want to check the author and commit information of this file line by line. To do that, I would run git blame as follows:

$ git blame gulpfile.babel.js

You should get something like this. Here, git blame shows the contents of the file including line numbers on the right. On the left of each line, git blame shows the commit hash, the author who is responsible for this commit, the date & time of the commit. A commit may change multiple lines of code in a source file. A commit may change only a single line of code in a source file. So, the same commit hash may appear for multiple lines. It may also appear only once. It depends on the commit.

From here, you can see which author changed what line of the source file. You can also see which commit is responsible for that change and when the change was made.

If the file has a lot of line, you can navigate using the <Up> and <Down> arrow keys. You can also quit the git blame window using the q key on your keyboard.

Now, if you want to learn more about what changed in a commit, simply copy the commit hash and use git log as follows.

$ git log -p 2ad70775

You should be able to see the full commit message, what lines are removed and what lines are added since the commit before it.

Displaying Author Email:

By default, git blame shows the author name. If you want to see the author email instead, run git blame with the -e option as follows:

$ git blame -e gulpfile.babel.js

As you can see, the author email is displayed instead of the author name.

Displaying Long Commit Hash:

By default, git blame shows short commit hashes which is unique as well. So, you can use it without any problem. But, if you prefer to see the full length commit hashes, then you can use the -l option of git blame as follows.

$ git blame -l gulpfile.babel.js

As you can see, the full length commit hashes are displayed instead of the short ones.

If you want, you can combine multiple options together as well to get the effect you want. For example, to see the author email and long commit hash, you can combine the -e and -l options together as follows:

$ git blame -el gulpfile.babel.js

Displaying Raw Timestamp:

By default, git blame shows a nicely formatted date & time. But, if for some reason, you need date & time as timestamp values, you can use the -t option of git blame as follows:

$ git blame -t gulpfile.babel.js

As you can see, the timestamp values are listed.

Listing Specific Range of Lines:

If you want to inspect only specific ranges of lines of a file using git blame, then you can use the -L option.

To display a range using the -L option, use git blame as follows:

$ git blame -L startLineNumber,endLineNumber filePath

For example, to display lines 1020 from the file gulpfile.babel.js, run git blame as follows:

$ git blame -L 10,20 gulpfile.babel.js

To display N number of lines starting from the line X, run git blame as follows:

$ git blame -L X,+N

For example, to display 5 lines starting from the line 15 of the file gulpfile.babel.js, run git blame as follows:

$ git blame -L 15,+5 gulpfile.babel.js

To display N number of lines before the line X, run git blame as follows:

$ git blame -L X,-N

For example, to display 5 lines before from the line 15 (including line 15) of the file gulpfile.babel.js, run git blame as follows:

$ git blame -L 15,-5 gulpfile.babel.js

Getting Help:

git blame has a lot more options. I covered the common ones in this article. If you need any help with any of the options of git blame, you can check the man page of git blame as follows:

$ man git-blame

Or,

$ git help blame

The man page of git blame.

So, that’s how you use git blame on your desired Git repository. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.