What is git blame?
The git blame
command is a tool that shows who last modified each line of a file, along with the commit hash and the timestamp of the change. It's a powerful feature for understanding the history of a file and tracking down when specific changes were made.
Despite its somewhat negative-sounding name, git blame
is not about assigning fault. Instead, it's a practical tool for discovering the context behind a piece of code. By identifying the author and the commit, you can better understand why a particular change was made.
When to Use git blame
You might find git blame
useful in several situations:
- Understanding Unfamiliar Code: When you encounter a complex or confusing piece of code,
git blame
can help you find the person who wrote it. They are likely the best person to ask for clarification. - Debugging: If you've found a bug,
git blame
can help you trace back when the problematic code was introduced. This can provide valuable clues about the original intent and help you fix the issue without introducing new problems. - Learning from the Codebase: By examining the history of a file, you can learn about the patterns and practices that have been used in the project. It's a great way to see how the code has evolved over time.
How to Use git blame
Using git blame
is straightforward. Simply provide the name of the file you want to inspect:
$ git blame my-file.js
The output will show each line of the file, prefixed with the commit hash, the author, and the timestamp of the last change.
Focusing on a Specific Range of Lines
If you're only interested in a specific part of a file, you can use the -L
option to specify a line range:
$ git blame -L 10,20 my-file.js
This command will show the blame information only for lines 10 through 20 of my-file.js
.
Ignoring Whitespace Changes
Sometimes, you might want to ignore changes that only affect whitespace. The -w
option tells git blame
to do just that:
$ git blame -w my-file.js
This can be helpful when you're trying to find the author of a functional change, rather than a stylistic one.
Tip
Using "git blame" in Tower
In the Tower Git client, you can access blame information with a single click. Simply open a file and switch to the "Blame" view to see the author of each line.
Ignoring Revisions in git blame
Sometimes, the output of git blame
can be cluttered by commits that don't add much context, like large-scale formatting changes or automated refactorings. These commits can hide the original author of a line, making it harder to trace the code's history.
To get a more meaningful history, you can tell git blame
to ignore specific revisions. This is particularly useful for excluding commits that only contain stylistic or non-functional changes.
You can achieve this by using the --ignore-rev
flag for a single commit or by creating a dedicated file with a list of commits to ignore and using the --ignore-revs-file
flag. This helps you focus on the significant changes and understand the true evolution of the code.
We cover this topic in depth in this blog post.
Final Words
git blame
is an essential tool for any developer working with Git. By providing a detailed history of a file, it helps you understand the context of the code, track down bugs, and learn from the evolution of the project.
It's not about blaming anyone; it's about understanding the story behind the code.
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.