0
0
GitHow-ToBeginner · 3 min read

How to See Commit History for a File in Git

Use the git log -- <filename> command to see the commit history for a specific file. This shows all commits that changed that file, including commit messages, authors, and dates.
📐

Syntax

The basic syntax to view commit history for a file is:

  • git log -- <filename>: Shows all commits that affected the specified file.
  • The -- separates options from the file path to avoid confusion.
bash
git log -- <filename>
💻

Example

This example shows how to see the commit history for a file named app.js in your Git repository.

bash
git log -- app.js
Output
commit 9fceb02d0ae598e95dc970b74767f19372d61af8 Author: Jane Doe <jane@example.com> Date: Mon Apr 1 10:00:00 2024 +0000 Fix bug in app.js commit 7ac9a8f1b2e3c4d5e6f7a8b9c0d1e2f3a4b5c6d7 Author: John Smith <john@example.com> Date: Sun Mar 31 15:30:00 2024 +0000 Add new feature to app.js
⚠️

Common Pitfalls

Common mistakes when viewing commit history for a file include:

  • Forgetting the -- before the filename, which can cause Git to misinterpret the command.
  • Using the wrong file path or filename, resulting in no commits shown.
  • Expecting git log without the filename to show file-specific history (it shows all commits in the repo).
bash
git log app.js  # Incorrect - missing '--'
git log -- app.js  # Correct
📊

Quick Reference

Summary tips for viewing commit history of a file:

  • Always use git log -- <filename> to focus on one file.
  • Use git log -p -- <filename> to see the actual changes (diffs) in each commit.
  • Use git log --follow -- <filename> to track history across file renames.

Key Takeaways

Use git log -- <filename> to see commit history for a specific file.
Include -- before the filename to avoid command errors.
Use git log --follow -- <filename> to track history through renames.
Add -p to see detailed changes in each commit.
Check the file path carefully to get accurate commit history.