Course contentsShow
← Software Engineering
Lesson 122 of 1,9333. Collaboration with GitFree lesson

Understanding File History with Git Log --follow

Track the complete history of a file across renames and moves to understand its evolution over time.

Understanding File History with Git Log --follow

You'll learn how to track a file's complete history even when it's been renamed or moved to different locations.

The Problem with Basic Git Log

When you use git log <filename> to see a file's history, Git only shows commits that affected that exact path. If the file was previously named something else or lived in another directory, those earlier commits disappear from view. It's like reading a person's biography but missing their childhood chapters because they changed their name.

What --follow Does

The --follow flag tells Git to trace a file backwards through renames and moves, reconstructing its complete lineage:

git log --follow path/to/current/file.js

This command walks through history looking for rename operations, connecting the dots between the file's current name and any previous names or locations.

Why This Matters

Files evolve. You might rename UserController.js to UsersController.js for consistency, or move utils.js from the root into a lib/ folder during refactoring. Without --follow, you'd lose visibility into why certain code exists or how bugs were introduced before the rename.

Common Use Cases

  • Understanding design decisions: "Why was this function written this way three years ago?"
  • Tracking down bug origins: "When did this problematic code first appear?"
  • Code archaeology: "Who originally created this module and what was its original purpose?"

Example Scenario

If src/components/Button.tsx was originally Button.js in the root directory, git log --follow src/components/Button.tsx reveals both the recent changes and the original creation and early development.

Key Takeaway: Use git log --follow to see a file's complete history across renames and moves, ensuring you never lose important context about code evolution.