Git blame allows you to find out who did a particular edit to a file... e.g., who is to blame. Git's not even being subtle about it!
The git blame tracking command checks the history of a file in your repository, and displays the author and the changes they made.
This lesson discusses how to run git blame on GitHub and your CLI.
Advantages Of Git Blame
git blame inspects particular parts of a file's history in order to determine the last author that modified that line. Therefore, git blame is really useful for projects where a lot of people work together.
By helping you easily find the person who made the changes in a file, you can directly ask them what they did rather than surfing through the code to understand it.
Note: Some text editor plugins build on top of git blame. For example, GitLense for VSCode and GitToolBox for IntelliJ's IDEs are two well-known plugins that use the git blame functionality to display inline information about the author for a current line of code.
Using GitHub's UI for Git Blame
You can also check the git blame of a file using GitHub. Their UI provides you with an intuitive window to this functionality. Let's take a look at how GitHub renders the git blame information on remote repositories on GitHub.
- Go to the Git Practice Repo
- Click on any specific file you want to inspect the history for, for example, the
NewFile1.mdfile - Click on the Blame button on that specified file
As you can see, GitHub shows the author's information, the time when the change was made, and which lines were added to the file.
Each time you click the icon, you'll see the previous revision information for that line, including who committed the change and when.
- In a file, you can also use the ... menu to view Git blame for a selected line:
After inspecting GitHub's collaborative UI to view git blame output in a nice and visual way, let's dive back into your local repository and look at the command's output on your CLI.
Using Your CLI for Git Blame
When you run git blame on your CLI, you can also view the revision history of lines within a file, just like on GitHub's UI.
Navigate to a local Git repository that has a history, and type:
$ git blame
If you run the git blame command like this, without any options, it will show you the command's manual so you can learn how to properly use it and what its options are:
Git Blame Example
Try the git blame command on your own.
- Clone the Git Practice Repo to your local machine, since you will need a repository with an existing commit history
- List the files, and make sure you can see a file called
hello-world.txt - Run the
git blamecommand on thehello-world.txtfile:
$ git blame hello-world.txt
You will receive some output from Git printed to your console, which will look like the below:
The information Git displays related to the changes in the file are:
- Unique hash number
- Author's name
- Time frame of edit
- Line number
- Content of the line
As you can see, git blame gives you a rich overview regarding the changes made to this file, and makes it easier for you to follow up with who applied the changes in the first place.
Git Blame Options
After giving the basic process of working with git blame a go, let's dive a little deeper into some additional options that the command provides. You can use this as a reference.
Limit: -L
$ git blame -L <start>,<end> <filename>
The -L option will limit the output to the specified line range. You can set the line range by passing two numbers:
First, the number of the first line you want to display Then, the number of the last line you want to see. For example, here we've limited the output to lines 1 through 6:
$ git blame -L 1,6 <filename>
In the picture below, you can see an example output for a file that contains some code:
This is useful if you want to focus on a specific part of a large file, as the output of git blame can otherwise get overwhelming.
Email: -e
$ git blame -e <filename>
The -e option displays the author's email address.
Full-Length Hashes: -l
$ git blame -l <filename>
git blame shows short unique commit hashes by default. This works fine. However, if you want to see the full-length commit hashes for some reason, you can use the -l option to display them.
Moved Lines from Other Files: -C
$ git blame -C <filename>
The -C option finds lines that were moved or copied from other files.
Moved Lines within this File: -M
$ git blame -M <filename>
The -M option also finds moved or copied lines, but only within the same file.
Ignore Whitespace: -w
$ git blame -w <filename>
The -w option ignores any whitespace changes that were done to the file.
For more information, check out Git's Git blame docs, and GitHub's tutorial on Tracking changes in a file.
Summary: What is Git Blame
git blameis a tracking command that displays the history of a file in order to find the author, and the changes they made.- You can check
git blameof a specific file using both the CLI and GitHub.
How to Check Git Blame on GitHub
- Go to the Git Practice Repo
- Click on any specific file you want to inspect the history for, for example the
NewFile1.mdfile - Click on the Blame button on that specified file