📚 Advanced Git & GitHub Topics
Estimated reading: 4 minutes 349 views

Git Blame – Find Out Who Changed What and When in Git


Introduction – Why Use Git Blame?

Need to know who last modified a specific line of code and when it happened? Whether you’re fixing a bug, reviewing a feature, or understanding legacy code, git blame is your go-to tool.

git blame shows detailed line-by-line information for a file, including:

  • The author who last changed each line
  • The commit hash and date
  • The line number and line content

In this detailed guide, you’ll learn:

  • How git blame works
  • Syntax and practical examples
  • Real use cases like debugging and audits
  • Output interpretation and GUI tools
  • Best practices and advanced flags

What Is git blame?

git blame annotates each line of a file with:

  • The commit hash where it was last changed
  • Author information
  • Date of change

It allows you to track code responsibility and changes over time.


Basic Syntax

git blame [options] <file>

Example:

git blame main.py

This will output each line of main.py along with:

  • The SHA of the commit that last touched it
  • Author
  • Timestamp
  • Line content

Example Output

3f7a1e2a (Alice  2025-06-16 10:12:33 +0530  1) def login():
d4c8f9ba (Bob    2025-06-15 15:45:10 +0530  2)     username = input("User: ")
3f7a1e2a (Alice  2025-06-16 10:12:33 +0530  3)     print("Welcome back!")

Breakdown:

  • 3f7a1e2a – commit hash
  • Alice – author
  • Date/Time – when it was committed
  • Line number and content

Practical Use Cases

1. Who changed this line?

git blame index.html

Inspect recent updates and identify responsible developers.


2. View blame with specific revision

git blame HEAD~1 app.js

Shows the state of app.js one commit before HEAD.


3. Narrow blame to a specific range of lines

git blame -L 25,40 script.py

Only shows blame for lines 25 through 40.


4. Ignore whitespace changes

git blame -w styles.css

Useful when formatting has changed but content hasn’t.


Useful git blame Options

OptionDescription
-L <start>,<end>Blame only a specific line range
--since=<date>Only consider commits after a certain date
--reverseShow commits that do not affect specified lines
-wIgnore whitespace when assigning blame
--show-emailShow full author email address
--show-nameShow full file path of origin
--date=relativeShow time like “2 weeks ago” instead of timestamp

Advanced Blame: Investigating Bugs

Let’s say you’re fixing a login issue in auth.js.

git blame -L 10,25 auth.js

This shows the authors and commits responsible for the logic within that range. Now you can:

  • Read the commit message
  • Use git show <hash> to see full commit
  • Ask questions if needed or trace the bug fix

Git Blame GUI Alternatives

Tired of the CLI output? Try these visual Git blame tools:

  • VS Code: Hover a line to see blame (with GitLens extension for full details)
  • GitKraken: Click a line in the file view to see its blame data
  • Sourcetree: Right-click file → “Annotate”
  • GitHub.com: View file → Click “Blame” button

Common Pitfalls to Avoid

  • Using blame on generated or minified files (won’t be helpful)
  • Assuming blame = fault — sometimes it’s just refactoring
  • Not filtering line ranges, which clutters the output

Best Practices

  • Combine git blame with git show for full context.
  • Use line range (-L) for large files to focus blame.
  • Use -w to ignore formatting-only commits.
  • Don’t treat blame output as blame—use it for insight and debugging.

Git Blame Summary Table

CommandDescription
git blame file.txtShow line-by-line change authorship
git blame -L 20,40 file.txtBlame lines 20–40 only
git blame -w file.txtIgnore whitespace differences
git blame --show-email file.txtShow email instead of name
git blame HEAD~3 file.txtBlame at a past revision
git blame --date=relative file.txtShow relative date like “2 weeks ago”

Git Blame Summary – Recap & Next Steps

git blame helps developers trace responsibility, debug issues, and understand how a file evolved—line by line. It’s not about blaming people, but about gaining context to make informed decisions.

Key Takeaways:

  • git blame shows who changed each line, when, and in which commit.
  • Use options like -L, -w, and --date=relative for refined control.
  • Pair it with git show, git log, or GUI tools for full context.

Real-World Relevance: Code reviewers, maintainers, and bug-fixers rely on git blame to dig into code history, understand logic changes, and keep quality high.


Git Blame — Frequently Asked Questions (FAQs)

How do I view blame for a specific range of lines?
Use:

git blame -L 15,30 app.js

Can I blame a file in a past commit?
Yes:

git blame HEAD~2 myfile.js

How do I ignore whitespace changes?
Add the -w option:

git blame -w index.css

How do I see the full email instead of author name?
Use:

git blame --show-email login.js

Is git blame available in GUI tools?
Yes. Tools like VS Code (GitLens), GitHub, and GitKraken support line-by-line blame.


Share Now :
Share

Git Blame

Or Copy Link

CONTENTS
Scroll to Top