Read Anthropic’s case study about Graphite Reviewer

How to use the `git blame` command

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git blame command is a powerful Git tool that tracks the origin of lines in a file. It annotates each line with metadata, including the commit hash, author, and timestamp. This command is particularly useful for:

  • Identifying the author of specific code changes.
  • Understanding the history behind code modifications.
  • Tracing the source of bugs or design decisions.

The basic syntax of the git blame command is:

Terminal
git blame [options] <file>

Here’s a breakdown of commonly used options:

  • -L <start>,<end>: Limits the output to a specific range of lines.
  • --date (e.g., relative, iso, short): Formats the date output.
  • -C or -M: Detects moved or copied lines from other files.

To see who modified each line in main.py:

Terminal
git blame main.py

Example output:

Terminal
6f5b4d3d (Alice 2024-12-10 10:32:14 -0800) def fetch_data():
74e2c4e9 (Bob 2024-12-11 14:01:02 -0800) return api.get_data()

To analyze lines 10–20 in main.py:

Terminal
git blame -L 10,20 main.py

Use the --date option for more readable timestamps:

Terminal
git blame --date=relative main.py

This might output:

Terminal
6f5b4d3d (Alice 2 weeks ago) def fetch_data():
74e2c4e9 (Bob 3 days ago) return api.get_data()

To detect if a line was copied or moved from another file:

Terminal
git blame -C -M main.py

This option is helpful for tracking code migrations during refactoring.

The Graphite CLI simplifies Git workflows, including commands like git blame. Here’s how you can integrate it:

While git blame operates on individual files, Graphite CLI helps manage stacked pull requests, making it easier to track and review changes. Suppose you want to analyze blame data for a file in a stacked PR:

  1. Checkout the relevant branch:

    Terminal
    gt checkout <branch_name>
  2. Identify the blamed changes:

    Terminal
    git blame -L 10,20 main.py

Graphite CLI's gt pr command can open pull requests where you can cross-reference git blame output. For instance:

Terminal
gt pr

This command opens the current PR in your browser. Combine this with git blame results to annotate feedback.

If git blame reveals outdated changes after syncing with main, Graphite CLI simplifies updates:

Terminal
gt sync

This command restacks open branches, ensuring your blame analysis reflects the latest commits.

  • Use with context: Combine git blame with git log for deeper insights into commit messages and histories.
  • Annotate during reviews: Use blame data to highlight specific changes needing attention in a pull request.
  • Automate workflows: Integrate Graphite CLI to streamline Git commands, making blame analysis part of your regular development cycle.

By using the git blame command and leveraging tools like Graphite CLI, you can uncover critical insights into code history and enhance your review process.

Built for the world's fastest engineering teams, now available for everyone