To list only the names of files changed between two commits in Git, use the git diff
command with the --name-only
flag. This provides a concise list of modified, added, deleted, or renamed files. Here’s how to do it:
Basic Syntax
git diff --name-only <commit1> <commit2>
Example:
git diff --name-only abc123 def456
# Outputs filenames changed between commits abc123 and def456
Key Variations
1. Compare Branches or Tags
git diff --name-only main..feature # Compare tips of two branches
git diff --name-only v1.0 v2.0 # Compare tags
2. Compare HEAD with a Previous Commit
git diff --name-only HEAD~3 HEAD # Changes in the last 3 commits
3. Compare Against a Specific Branch
git diff --name-only origin/main # Compare current branch to remote main
4. Include Merge Base (For Branches)
git diff --name-only main...feature # Compare feature against merge base of main
Options
--diff-filter=<filter>
: Filter by type of change (e.g.,A
for added,D
for deleted,R
for renamed).
Example:
git diff --name-only --diff-filter=AMR <commit1> <commit2>
Examples
1. Files Changed in the Last Commit
git diff --name-only HEAD~1 HEAD
2. Files Changed Between Two Branches
git diff --name-only develop main
3. Files Added Between Tags
git diff --name-only --diff-filter=A v1.0 v2.0
How to Find Commit Hashes
Use git log
to identify commits:
git log --oneline # Shows abbreviated commit hashes and messages
Notes
- Order of Commits: Swapping
<commit1>
and<commit2>
reverses the diff direction but lists the same filenames. - Renamed Files: Shows the new filename (use
--name-status
for rename details). - Untracked Files: Not included—this command only compares committed changes.
Summary
- Use
git diff --name-only <commit1> <commit2>
for a clean list of changed files. - Add
--diff-filter
to focus on specific types of changes (e.g., additions, deletions). - Combine with branch/tag names or relative references like
HEAD~
for flexibility.