When you inherit a codebase, understanding WHY code exists is often more valuable than WHAT it does. Git history is your time machine.
The Basic Timeline
# Get simple commit history for a file
git log --follow --all --pretty=format:"%h|%ai|%an|%s" -- app/Services/ReportGenerator.php
Output:
f4bc663|2018-05-09 17:46:43 +0800|John Smith|TASK-707 Add report tracking
201105b|2019-03-27 10:02:08 +0800|Jane Doe|Refactor - add dependency injection
The Full Story (Patches + Stats)
# See actual code changes with statistics
git log --follow --all --stat --patch -- app/Services/ReportGenerator.php
What You Learn
- Original author: Who to ask if you have questions
- Creation date: How old this code is (affects modernization priority)
- Commit message: Why it was added (often references a ticket/task)
- Evolution pattern: Was it written once and forgotten, or actively maintained?
- Refactoring history: What patterns were replaced (helps avoid repeating mistakes)
Reading the Story
The example above tells us:
- Created in May 2018 for admin dashboard tracking
- Refactored in March 2019 to add dependency injection
- One year gap between changes suggests low-priority maintenance code
- Two different authors = knowledge might be spread across team
Pro Tips
- Use
--follow: Tracks files even if renamed (critical!) - Filter by date:
--since="2020-01-01"to see recent changes only - Check for deletions: If no commits since 2020, might be deprecated
- Look for patterns: Frequent refactors = evolving requirements
When to Use This
- Before refactoring legacy code (understand why it’s weird)
- When debugging mysterious behavior (was it always like this?)
- During code review (does this change make sense given the history?)
- When deciding whether to delete code (has it been touched recently?)
Next time you see weird legacy code, don’t guess — check the git history. It often explains everything.