Course
Git is a powerful tool for tracking code changes, particularly helping teams track them, collaborate efficiently, and debug issues. It provides various tools for managing code history; one of the most useful is git blame
. This command helps developers trace the origin of every line in a file, showing who last modified it and when.
In this article, we will explore how to use git blame
to track changes in a project. We will start with basic commands and gradually move into advanced features such as ignoring specific revisions, integrating with development tools, and using best practices. By the end, you'll have a solid understanding of how to analyze code history effectively and improve collaboration within your team.
What is Git Blame?
Code changes can be difficult to track, especially in large projects where multiple contributors are involved. Understanding the history of a file is crucial for debugging, reviewing past decisions, and maintaining code quality. Without a clear history, teams may struggle to determine why a particular change was made or who to ask for clarification.
This is where git blame
becomes invaluable. It allows you to pinpoint changes at a granular level, revealing commit details alongside each line of code. With this information, you can quickly find the relevant commit, check the associated message, and see the broader context of the change. Whether you are troubleshooting a bug, reviewing modifications, or simply trying to understand the evolution of a file, git blame
provides the insights needed to make informed decisions.
Why use git blame?
When working in a team or debugging an issue, knowing who last edited a line of code can be helpful. If a bug appears after a recent update, git blame
helps pinpoint the exact change that introduced the issue. It also allows developers to reach out to the right person when they need clarification about a specific issue.
This command is also useful for understanding the reasoning behind changes. Code comments and commit messages sometimes fail to capture the full thought process. By tracing a change back to a specific commit, developers can review related discussions and see the broader picture.
You can learn more in our Git and GitHub tutorial, which serves as a beginner's guide, explaining the workings of Git version control and highlighting its crucial role in data science projects.
Basic usage of git blame
The basic syntax of git blame
is:
git blame <file>
This command annotates each line with the last commit that modified it, including author, timestamp, and commit hash. Here’s an example:
$ git blame example.py
derrickmwiti@Derricks-MacBook-Pro remixer % git blame src/components/ContentGenerator.vue
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 1) <template>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 2) <div class="content-generator">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 3) <div class="card h-100 border-0 shadow-sm">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 4) <div class="card-header bg-white border-bottom-0 pt-4">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 5) <div class="d-flex justify-content-between align-items-center mb-3">
289030a0 (Derrick Mwiti 2025-03-11 12:47:11 +0300 6) <div class="d-flex align-items-center gap-2">
289030a0 (Derrick Mwiti 2025-03-11 12:47:11 +0300 7) <h5 class="mb-0">Content Generator</h5>
289030a0 (Derrick Mwiti 2025-03-11 12:47:11 +0300 8) <span class="badge bg-secondary" v-if="openaiModel">{{ openaiModel }}</span>
289030a0 (Derrick Mwiti 2025-03-11 12:47:11 +0300 9) </div>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 10) </div>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 11)
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 12)
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 13) <nav>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 14) <div class="nav nav-tabs card-header-tabs gap-1" id="nav-tab" role="tablist">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 15) <button
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 16) v-for="platform in platforms"
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 17) :key="platform"
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 18) class="nav-link"
Each line contains:
- The commit hash (
^3b1aef2
and^8f2c4d3
) - The author’s name (Derrick Mwiti)
- The timestamp of the last modification
- The actual line of code
Practical Applications of Git Blame
Now that you understand the basic syntax of git blame, let’s look at how you can apply it in everyday development scenarios.
Finding the author of a specific line
If a file is large and you only need to inspect a certain part, specify a line range:
derrickmwiti@Derricks-MacBook-Pro remixer % git blame -L 10,20 src/components/ContentGenerator.vue
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 10) </div>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 11)
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 12)
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 13) <nav>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 14) <div class="nav nav-tabs card-header-tabs gap-1" id="nav-tab" role="tablist">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 15) <button
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 16) v-for="platform in platforms"
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 17) :key="platform"
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 18) class="nav-link"
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 19) :class="{ active: activeTab === platform }"
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 20) :id="nav-${platform}-tab"
This focuses on lines 10 to 20, making it easier to locate relevant changes.
Understanding changes using git show
While git blame
highlights the last change to each line, it doesn’t explain what was modified in that commit. To see more details, use git show <commit-hash>
:
derrickmwiti@Derricks-MacBook-Pro remixer % git show 28203e0
commit 28203e0f7cbe92c6baf6897a54686e87d16c9cda
Author: Derrick Mwiti <mwitiderrick@gmail.com>
Date: Mon Mar 10 17:48:31 2025 +0300
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..5a5809d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,9 @@
+[*.{js,jsx,mjs,cjs,ts,tsx,mts,cts,vue,css,scss,sass,less,styl}]
+charset = utf-8
+indent_size = 2
+indent_style = space
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+end_of_line = lf
+max_line_length = 100
diff --git a/.env b/.env
:
Replacing <commit-hash>
with the actual commit ID from git blame
reveals the full commit message and code diff, helping to understand the reason behind the change.
To explore this further, explore our Git Diff tutorial to learn how to use git diff to track code changes effectively, from basic comparisons to advanced techniques.
Git Blame Advanced Features
Here are some of the more involved ways that you can utilize Git blame in a workflow:
Ignoring whitespace changes
Some formatting changes, such as fixing indentation, do not affect logic. To filter these out, use git blame -w <file>
:
derrickmwiti@Derricks-MacBook-Pro remixer % git blame -w src/components/ContentGenerator.vue
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 1) <template>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 2) <div class="content-generator">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 3) <div class="card h-100 border-0 shadow-sm">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 4) <div class="card-header bg-white border-bottom-0
Ignoring specific revisions
If certain commits (such as bulk reformatting) clutter the blame history, they can be ignored with git blame --ignore-rev <commit-hash> <file>
:
derrickmwiti@Derricks-MacBook-Pro remixer % git blame --ignore-rev 28203e0 src/components/ContentGenerator.vue
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 1) <template>
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 2) <div class="content-generator">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 3) <div class="card h-100 border-0 shadow-sm">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 4) <div class="card-header bg-white border-bottom-0 pt-4">
^28203e0 (Derrick Mwiti 2025-03-10 17:48:31 +0300 5) <div class="d-flex justify-content-between align-items-center mb-3">
289030a0 (Derrick Mwiti 2025-03-11 12:47:11 +0300 6) <div class="d-flex align-items-center gap-2">
To ignore multiple commits consistently, you can list them in a file and reference it using the --ignore-revs-file
option. This helps maintain a clean blame history by excluding irrelevant changes.
In your repository's root directory, create a file named .git-blame-ignore-revs
. List the commit hashes to ignore, optionally adding comments for clarity:
# Commit introducing code formatting changes
a926bba49c89a5b882cd298be3af2570b1e6252c
Execute the following command to ignore the specified revisions:
git blame --ignore-revs-file .git-blame-ignore-revs filename
Run the following command to instruct Git to use your ignore file by default:
git config blame.ignoreRevsFile .git-blame-ignore-revs
Viewing author emails instead of names
To display the author's email instead of their name:
derrickmwiti@Derricks-MacBook-Pro remixer % git blame --show-email src/components/PostsManager.vue
dbe547ec (<derrick@gmail.com> 2025-03-11 09:22:59 +0300 1) <template>
dbe547ec (<derrick@gmail.com> 2025-03-11 09:22:59 +0300 2) <div class="posts-manager">
dbe547ec (<derrick@gmail.com> 2025-03-11 09:22:59 +0300 3) <h3>Saved Posts</h3>
dbe547ec (<derrick@gmail.com> 2025-03-11 09:22:59 +0300 4)
dbe547ec (<derrick@gmail.com> 2025-03-11 09:22:59 +0300 5) <div v-if="loading" class="text-center my-4">
dbe547ec (<derrick@gmail.com> 2025-03-11 09:22:59 +0300 6) <div class="spinner-border text-primary" role="status">
This can be useful for contacting the person responsible for a specific change.
Displaying full commit hashes
By default, git blame
shows short commit hashes. To see the full hashes, run:
derrickmwiti@Derricks-MacBook-Pro remixer % git blame --abbrev=40 src/components/PostsManager.vue
dbe547ec4ece7cb2a1ff3dadb4770cc9ee9f161e (Derrick Mwiti 2025-03-11 09:22:59 +0300 1) <template>
dbe547ec4ece7cb2a1ff3dadb4770cc9ee9f161e (Derrick Mwiti 2025-03-11 09:22:59 +0300 2) <div class="posts-manager">
dbe547ec4ece7cb2a1ff3dadb4770cc9ee9f161e (Derrick Mwiti 2025-03-11 09:22:59 +0300 3) <h3>Saved Posts</h3>
dbe547ec4ece7cb2a1ff3dadb4770cc9ee9f161e (Derrick Mwiti 2025-03-11 09:22:59 +0300 4)
dbe547ec4ece7cb2a1ff3dadb4770cc9ee9f161e (Derrick Mwiti 2025-03-11 09:22:59 +0300 5) <div v-if="loading" class="text-center my-4">
You can adjust the length of Git blame abbreviations using the --abbrev=<n>
option.
Using git blame in Visual Studio Code
For developers using Visual Studio Code, the Git Blame extension provides built-in support for git blame
.
This extension allows users to:
- View blame annotations inline.
- Hover over lines to see commit details.
- Access full file and line history.
Git Blame Best Practices
Maintaining a .git-blame-ignore-revs
file ensures that formatting commits don’t clutter the blame history. When working in a team, it helps if everyone follows the same conventions for ignoring revisions.
When using git blame
, the goal should always be to understand the reasoning behind changes, not to assign blame. It’s a tool for learning and improving collaboration, helping developers work more efficiently together.
Conclusion
In this article, we've covered how git blame
can help developers track changes in their code. From basic usage to advanced features, we explored how this command provides clarity on who modified a file and when.
You can optimize this command by integrating it with other Git tools, such as git show
and configuring ignored revisions. Whether you're debugging an issue, reviewing changes, or simply understanding a file’s history, git blame
offers valuable insights that improve code maintenance and collaboration.
Mastering tools like git blame
helps you navigate codebases with confidence, collaborate more effectively, and maintain cleaner project histories. It's not just about finding out who changed a line—but why—and using that insight to build better software. To dive deeper into the Git ecosystem, check out our comprehensive Git course. or dive into our Git course for intermediate users.
Git Blame FAQs
How can I ignore whitespace changes in git blame?
You can use the -w
option to ignore changes that only involve whitespace, such as indentation or spacing adjustments.
How do I find the commit details of a specific change in git blame?
After identifying a commit hash from git blame, use git show to see the commit details, including the changes made.
How can I exclude certain commits from git blame results?
If you want to ignore specific commits (e.g., code formatting changes), use:
git blame --ignore-rev <commit-hash> <file>
For multiple ignored commits, store them in a .git-blame-ignore-revs
file and run:
git blame --ignore-revs-file .git-blame-ignore-revs <file>
Is there a way to use git blame in Visual Studio Code?
Yes, the Git Blame extension in VS Code provides inline blame annotations, commit history, and detailed insights. You can enable or customize blame annotations through its settings.