Lập trình · 24/09/2026

Git Blame: Understand Change Context Before Editing Code

A condition looks redundant, yet removing it breaks an uncommon case. Git history can help recover the context before you edit. Git blame is an investigation starting point, not a verdict about who caused a bug.

Git blame: Tìm bối cảnh thay đổi trước khi sửa code

A condition looks redundant, yet removing it breaks an uncommon case. Git history can help recover the context before you edit. Git blame is an investigation starting point, not a verdict about who caused a bug.

1. What does blame tell you?

The git-blame manual describes attribution of lines to the revision and author that last modified them. It does not explain business requirements or prove where a defect originated.

A line may have moved, been reformatted or changed during a refactor. Use the commit identifier to investigate further rather than stopping at the displayed name.

2. Start with a small region

Run from the repository root, replacing this illustrative path and range:

git status --short
git blame -L 40,65 HEAD -- src/InvoiceService.php

HEAD explicitly selects the current commit. Uncommitted edits can make editor line numbers differ from that version, so locate the correct region first.

A narrow range preserves a concrete question, such as when invoice-date handling appeared. Scanning an entire large file and picking an author rarely identifies the cause.

3. Inspect the commit and supporting evidence

Replace the illustrative abc1234 with the actual commit:

git show --stat abc1234
git show abc1234 -- src/InvoiceService.php

Read the message, patch and related file list. Look for tests or documentation changed together. Follow issue or pull-request references where authorized to understand the alternatives considered.

Missing tests do not prove a condition is unnecessary. Distinguish evidence from hypotheses. For merge commits, consider diff presentation and parent relationships before interpreting the change.

4. Reduce whitespace noise

git blame -w -L 40,65 HEAD -- src/InvoiceService.php

The -w option ignores whitespace changes when tracing lines. This can help see past formatting work, but it does not understand program semantics. Compare ordinary output when whitespace matters to the content.

If the commands identify different commits, inspect both. The older commit is not automatically the right answer.

5. Search history for deleted code

Blame on the current version does not list vanished lines. git log can search changes using strings or patterns:

git log -p -S 'legacyTaxMode' -- src/InvoiceService.php
git log -p -G 'legacyTaxMode' -- src/InvoiceService.php

-S finds changes in the number of occurrences of a string; -G finds added or removed patch lines matching a regular expression. They are not equivalent, so one empty result does not prove that the text was never changed.

A path filter can miss history under another filename. For suspected renames, inspect a single file with:

git log --follow -p -- src/InvoiceService.php

Rename following has limitations, particularly with complex history. A clone missing history also cannot show commits it does not contain.

6. Turn findings into tests

Suppose you discover that a condition was introduced for an invoice adjustment. Create a test describing that case, establish current behavior and evaluate the proposed edit. History suggests a hypothesis; tests check it against today's system.

  • Inspect the correct revision and code region.
  • Read the commit, not just the author.
  • Look for supporting tests, requirements or documentation.
  • Separate evidence from speculation.
  • Verify that the edit preserves required behavior.

Ask why the code exists, not merely who wrote it. Turning recovered context into tests and useful notes makes future maintenance less dependent on team memory.

Discussion

Comments 0

Sign in to comment

You need an account to join the discussion and reply to other readers.

Sign inRegister

No comments yet. Be the first to share your thoughts.