How to filter commits by file in Git

Understanding the complete history of a specific file or directory is essential for debugging, code review, and tracking feature development. With over 25 years of software development experience and as the creator of CoreUI, I regularly investigate file histories to understand code evolution. Git log accepts file paths as arguments to filter commits that modified those specific files or directories. This approach reveals who changed a file, when, and why, making file-level archaeology straightforward.

Use git log with file paths to view commits that modified specific files or directories.

# View commits that changed a specific file
git log -- path/to/file.js

# Show commits with patches for a file
git log -p -- path/to/file.js

# One-line summary for file
git log --oneline -- path/to/file.js

# Show commits for multiple files
git log -- file1.js file2.js

# Show commits for all files in directory
git log -- src/components/

# Show commits with file statistics
git log --stat -- path/to/file.js

# Show commits with author and date
git log --pretty=format:'%h %an %ad %s' --date=short -- file.js

# Follow file through renames
git log --follow -- path/to/file.js

# Show who changed each line (blame with history)
git log -L 10,20:path/to/file.js

# Show commits in date range for file
git log --since='2025-01-01' -- path/to/file.js

# Show commits by author for file
git log --author='John' -- path/to/file.js

# Show first commit that created file
git log --diff-filter=A -- path/to/file.js

# Show commits that deleted file
git log --diff-filter=D -- path/to/file.js

# Show commits that modified file
git log --diff-filter=M -- path/to/file.js

# Combine filters
git log --author='John' --since='2025-01-01' -p -- src/components/

Find when file was deleted:

git log --all --full-history -- path/to/deleted/file.js

View file at specific commit:

git show commit-hash:path/to/file.js

Best Practice Note

Use --follow to track file history through renames—essential for files that have been moved or renamed. The -- separator prevents ambiguity between file paths and branch names. Use -L to show history of specific line ranges, perfect for understanding when a function changed. Combine with --stat to see how much the file changed in each commit. The --diff-filter option filters by change type (A=added, D=deleted, M=modified, R=renamed). This is how we track CoreUI component evolution—filtering by file to understand feature development, identify bug introductions, and review refactoring history.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author