Lesson 23 — Advanced GitHub Blame, Commit History, and Line-Level Investigation¶
Module: 1 — Code
Level: Advanced
Type: Practical investigation
Repository: github-deep-dive-training
1. Learning objectives¶
By the end of this lesson, you should be able to:
- Trace a line of code through its commit history.
- Understand GitHub Blame,
git blame,git log -L, andgit log --follow. - Investigate file renames and code movement.
- Connect commits to pull requests and issues.
- Compare historical implementations.
- Identify formatting-only commits that affect blame results.
- Build an evidence-based explanation of why code changed.
- Create permanent GitHub links to investigation evidence.
2. Investigation workflow¶
When investigating unfamiliar code:
- Locate the relevant file, function, or line.
- Inspect its blame information.
- Identify the commit that last modified it.
- Inspect the commit's complete diff.
- Find the related PR or issue, if available.
- Trace earlier changes.
- Compare historical implementations.
- Document the evidence using permanent links.
Important: Blame identifies the commit that last modified a line. It does not necessarily identify the original author or explain why the code exists.
3. GitHub Blame versus Git history¶
| Tool | Main purpose |
|---|---|
| GitHub Blame | Shows which commit last changed each line |
git blame |
Inspects line-level modification history |
git log -- path |
Shows commits affecting a file |
git log -L |
Traces a function or line range |
git log --follow |
Follows file history across renames |
git show |
Inspects a specific commit |
git diff |
Compares two revisions |
4. Investigating code with GitHub¶
- Open the repository's Code tab.
- Navigate to a source file.
- Open Blame.
- Select a line.
- Open its associated commit.
- Inspect the diff, commit message, author, and date.
- Find a related PR or issue, if available.
- Record the evidence.
Record:
- File path and line number.
- Commit SHA.
- Commit message.
- Author and date.
- Changes shown in the diff.
- Related PR or issue.
- Explanation supported by the evidence.
5. Git CLI investigation commands¶
5.1 Inspect blame¶
5.2 Inspect commits¶
5.3 Inspect file history¶
git log -- path/to/file.py
git log --oneline -- path/to/file.py
git log -p -- path/to/file.py
git log --follow --oneline -- path/to/file.py
5.4 Trace a function¶
5.5 Trace a line range¶
git log -L has limitations for certain history and path changes. It is not a universal code-movement detector.
6. Detecting renames and code movement¶
Detect file renames¶
Git detects renames using similarity heuristics. A reported similarity percentage is an estimate, not proof that the file's behavior remained unchanged.
Detect moved lines¶
Detect copied lines¶
These options help detect moved or copied lines. Their results are heuristic.
When investigating a rename or refactor, check:
- The original file path.
- The new file path.
- Whether functions were renamed.
- Whether implementation details changed.
- Whether tests changed.
- Whether the refactor had an associated PR.
7. Formatting commits and blame-ignore revisions¶
Formatting commits can cause blame to attribute many lines to a mechanical formatting change.
A repository can maintain .git-blame-ignore-revs:
## Repository-wide formatting
<full-formatting-commit-sha>
## Line-ending conversion
<full-line-ending-commit-sha>
Run:
Configure Git for the repository:
Verify:
Only include commits that genuinely represent mechanical changes. Do not ignore commits that also introduced meaningful behavior changes.
GitHub can use this file in its blame view when correctly configured and supported.
8. Connecting commits to pull requests and issues¶
A commit describes a change. A pull request can explain the motivation, requirements, alternatives, and review discussion.
Investigate:
- The commit message.
- The commit diff.
- The associated PR, if available.
- The PR description.
- Review comments and requested changes.
- Linked issues.
- Tests added or modified.
Useful commit-message searches:
git log --oneline --all --grep="duplicate order"
git log --all --grep="token validation" --oneline
git log --all --grep="payment" --regexp-ignore-case --oneline
Example issue references:
Fixes #42 can automatically close the referenced issue when the PR is merged into the repository's default branch, subject to GitHub's closing-reference rules.
Not every commit has an associated PR or issue.
9. Comparing historical versions¶
Compare two commits:
Compare one file:
Display a summary:
Read an old version:
Compare a file across releases:
List commits between releases affecting a file:
10. Practical investigation scenarios¶
Scenario A — Unexpected validation rule¶
You find:
def validate_quantity(self, value):
if value <= 0:
raise serializers.ValidationError(
"Quantity must be greater than zero."
)
return value
Investigate:
- Which commit last modified this validation?
- What did the previous implementation look like?
- Was the rule introduced to fix a bug?
- Did the PR add tests?
- Is the implementation consistent with the business requirements?
Commands:
git blame -L 1,12 backend/orders/serializers.py
git log --oneline -- backend/orders/serializers.py
git log -L :validate_quantity:backend/orders/serializers.py
Replace the example paths with paths from your project.
Scenario B — A function was moved¶
You find:
Trace its history:
Inspect a suspected refactoring commit:
Determine whether the implementation was simply moved or whether its behavior changed.
Scenario C — Blame attributes everything to formatting¶
- Inspect the formatting commit.
- Verify that it was mechanical.
- Add its SHA to
.git-blame-ignore-revs. - Configure Git to use the file.
- Compare blame results before and after.
Scenario D — A regression appeared between releases¶
Find relevant release tags:
Compare the releases:
List commits affecting the relevant file:
Inspect the changes to narrow down the investigation.
11. Practical exercises¶
Exercise 1 — Find the last modifying commit¶
- Open a Markdown or source file.
- Open its GitHub Blame view.
- Select a line.
- Record its commit SHA and message.
- Inspect the commit diff.
Deliverable: Commit SHA, line number, and explanation.
Exercise 2 — Trace a file's history¶
Find at least three commits that changed the file.
Deliverable: Chronological change summary.
Exercise 3 — Investigate a commit¶
Identify the files changed, additions, deletions, and purpose.
Deliverable: Commit investigation note.
Exercise 4 — Trace a function¶
Identify at least two meaningful changes.
Deliverable: Function evolution timeline.
Exercise 5 — Investigate a rename¶
Determine whether the file has been renamed.
Deliverable: Old and new paths, if applicable.
Exercise 6 — Detect moved code¶
Compare the output.
Deliverable: Explanation of the differences.
Exercise 7 — Compare historical versions¶
Identify important behavior changes.
Deliverable: Before-and-after explanation.
Exercise 8 — Find the PR or issue¶
Choose a commit introduced through a pull request.
Find the PR and identify the requirement or issue it addressed.
Deliverable: PR number, issue number if available, and explanation.
Exercise 9 — Create a blame-ignore file¶
If the repository has a genuine formatting-only commit:
- Create
.git-blame-ignore-revs. - Add its full SHA.
- Configure Git.
- Compare blame results.
Deliverable: Ignore file and evidence.
Exercise 10 — Create permanent investigation links¶
Create a Markdown note containing:
- Permanent file link.
- Permanent blame link.
- Commit link.
- PR or issue link, if available.
Deliverable: Working links and investigation notes.
12. Advanced challenge — Investigate a real code change¶
Choose a function from a Django REST Framework or React project with a meaningful history.
Reconstruct its development history.
Requirements:
- Identify the current implementation.
- Find its latest modifying commit.
- Inspect the complete diff.
- Trace its history using
git log -L. - Check whether the file was renamed or code was moved.
- Find the related PR and issue, if available.
- Compare an earlier implementation with the current one.
- Explain the reason for the change using evidence.
Create a report:
## Code Investigation Report
## 1. Target
- Repository:
- File:
- Function:
- Current line range:
## 2. Latest Modification
- Commit SHA:
- Commit message:
- Author:
- Date:
## 3. Historical Changes
| Commit | Change | Reason |
|---|---|---|
| ... | ... | ... |
## 4. Related Pull Request / Issue
- PR:
- Issue:
- Original requirement:
## 5. Before and After
- Previous behavior:
- Current behavior:
- Important differences:
## 6. Conclusion
Explain what changed and why, citing the evidence.
## 7. Permanent Links
- File:
- Blame:
- Commit:
- Pull request:
- Issue:
13. Investigation principles¶
- Blame is evidence, not an explanation.
- Commit messages must be verified against the diff.
- A rename does not prove behavior was preserved.
- PR descriptions may omit important details.
- Review comments and linked issues can provide important context.
- Formatting commits can obscure blame history.
- Permanent investigation links should use commit SHAs rather than moving branch names.
14. Completion checklist¶
- [ ] I can use GitHub Blame.
- [ ] I can inspect the commit behind a line.
- [ ] I can trace a file's history.
- [ ] I can trace a function with
git log -L. - [ ] I understand rename and copy detection.
- [ ] I can use
.git-blame-ignore-revs. - [ ] I can connect commits to PRs and issues.
- [ ] I can compare historical implementations.
- [ ] I have completed the practical exercises.
- [ ] I have completed the advanced investigation report.
Next lesson: Lesson 24 — GitHub Releases, Tags, and Version History: Advanced Repository Release Management.