How to Use the git blame Command
You may have been told in school never to play the blame game. When you’re working with Git, you can ignore that advice. Using the blame command is an essential part of Git.
The git blame command analyzes each line in a file to determine who last modified the line and in what commit the line was modified.
In this guide, we’re going to talk about the git blame command and how it works. We’ll walk through a few examples of this command to help you get started. Let’s begin!
What is git blame?
The git blame command returns a list of all the lines in a file alongside who changed that file and when it was last changed. This command is useful because it helps you see how a file has evolved without having to look through different commits individually.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
To use this command, we’re going to need a repository with some commits. Let’s use the Career Karma web-tutorials repository as an example. We’ll need to clone this repository to our computer so we can get started:
git clone https://github.com/Career-Karma-Tutorials/web-tutorials
Now that we have this repository on our local machine, we can start using the git blame command.
How to Use git blame
The git blame command shows the history of an individual file. It cannot be used on multiple files. Let’s analyze the history of the README.md file in the repository:
git blame README.md
This file has been changed in a few different commits:
ef070f80 (jamesgallagher432 2020-06-26 12:57:37 +0100 1) # Career Karma Web Tutorials ef070f80 (jamesgallagher432 2020-06-26 12:57:37 +0100 2) ef070f80 (jamesgallagher432 2020-06-26 12:57:37 +0100 3) This repository contains the code for Career Karma's web tutorials. ef070f80 (jamesgallagher432 2020-06-26 12:57:37 +0100 4) ef070f80 (jamesgallagher432 2020-06-26 12:57:37 +0100 5) Check out our technical tutorials on the [Career Karma blog](careerkarma.com/blog/). ef070f80 (jamesgallagher432 2020-06-26 12:57:37 +0100 6) bad0b91c (James Gallagher 2020-07-21 09:45:46 +0100 7) ## Articles
This shows the first seven lines of our README.md file, line by line. There are more lines to show but we don’t need to analyze every line for this tutorial.
There’s a lot of information we can tell from this output. The first line of our file contains the following metadata:
- Commit ID: ef070f80
- Author: jamesgallagher432
- The date on which the commit was created: 2020-06-26 12:57:37 +0100
- Line number: 1
- Contents of the line: # Career Karma Web Tutorials
This data is shown for every line in our project. This tells us that jamesgallagher432 authored the first line in the repository. It was part of the commit ef070f80.
Suppose you wanted to know who added the “## Articles” heading to the document. All you need to do is check its entry in git blame:
bad0b91c (James Gallagher 2020-07-21 09:45:46 +0100 7) ## Articles James Gallagher last modified the line in the commit bad0b91c.
You can take a broader view of the git blame command to learn more about our repository.
There are two authors listed in our blame: James Gallagher and jamesgallagher432. This means that two people have changed the file. There are two different commits in the blame. This means that the file has only been amended twice.
How to View Particular Lines
By default, the git blame command analyzes every line in a file. This is impractical if the file with which you are working is dozens, hundreds, or thousands of lines long.
You can view a range of lines using the -L option:
git blame -L 9,12 README.md
This will return all the lines between the range of 9 and 12 in the README.md file.
How to View Moved and Copied Lines
The git blame command allows you to see whether a line has been moved or copied from both the same file or a different file.
The -M option checks whether a line has been moved or copied in the same file; the -C option checks if a line was moved from another file.
If a line has been moved or copied, the person who wrote those lines will be listed as the author. The person who moved the lines is not the author.
Consider this command:
git blame -M README.md
When you execute this command, all the lines of code inside the README.md file that have been moved or copied within the file will be denoted.
Git Log vs. Git Blame
git log and git blame both have similar purposes. They are used to analyze the history of a project. The git log command shows a list of all the commits made to a repository.
The git blame command is most useful when you want to see who last modified a line. It’s impossible to find out how a line has evolved over time using Git blame.
That’s where the git log command comes in handy. Git log can be used to check who has changed a particular line of text in a file:
git log -S "## Articles" README.md
This command produces a list of all the commits that affect the “## Articles” line in the README.md file:
commit bad0b91c5bf9f5fc28ef8753578376e925182b2b (HEAD -> master, origin/master) Author: James Gallagher <email> Date: Tue Jul 21 09:45:46 2020 +0100 docs: Add article list to README.md
Only one commit has changed this file. The commit in which the “## Articles” line was added was bad0b91c. The commit was pushed by James Gallagher. If multiple changes had been made to this line of text, more commits would be listed.
The git blame command tells you when a line of text in a file was last changed. It also tells you in what commit the line of text was changed and who made the change.
The git blame command is most useful when you want to find out about the most recent revisions made to a file. You can use the git log command if you want to see how a file has evolved over time.
Now you’re ready to use the git blame command like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.