NeuroAgent

Git Diff Name Only: List Changed Files Between Commits

Learn how to use git diff --name-only to list only file names changed between two Git commits. Perfect for automation, deployment scripts, and code review preparation.

Question

How to list only the names of files that changed between two commits in Git

I have a repository with multiple commits and need to identify which files were modified between two specific commits, identified by their SHA1 and SHA2 hashes. What Git command should I use to display only the names of the changed files without any additional information?

NeuroAgent

To list only the names of files that changed between two specific commits in Git, use the git diff --name-only command followed by the two commit hashes. For example: git diff --name-only SHA1 SHA2 will display just the file paths without any diff content, line numbers, or other metadata.

Contents

Basic Usage of git diff --name-only

The git diff --name-only command is the primary tool for listing changed files between commits. Its basic syntax is straightforward:

bash
git diff --name-only <commit1> <commit2>

Where:

  • <commit1> is the first commit hash (SHA1)
  • <commit2> is the second commit hash (SHA2)

This command will output only the file paths that were modified between these two commits, with each file name appearing on a separate line.

Understanding the Output

The output shows the relative paths to files from the repository root. For example:

src/main.js
components/Header.jsx
README.md

Each line represents a file that was added, modified, or deleted between the two commits. The command doesn’t show the actual content changes, just the file names.

Working with Commit Ranges

You can also use branch names or tags instead of commit hashes:

bash
git diff --name-only main develop
git diff --name-only v1.0.0 v2.0.0

If you omit the second commit, Git will compare against the working directory:

bash
git diff --name-only HEAD~5  # Compare with 5 commits ago

Advanced Options and Variations

The git diff --name-only command supports several useful options that can enhance its functionality:

Filtering File Types

You can combine git diff --name-only with other Git tools to filter the results:

bash
# Show only changed JavaScript files
git diff --name-only SHA1 SHA2 | grep '\.js$'

# Show only changed files in specific directories
git diff --name-only SHA1 SHA2 | grep '^src/'

Using with Other Git Commands

The output of git diff --name-only can be piped to other commands:

bash
# Count changed files
git diff --name-only SHA1 SHA2 | wc -l

# Process each changed file
git diff --name-only SHA1 SHA2 | while read file; do
    echo "Processing: $file"
done

Additional Useful Flags

bash
# Show only files that existed in both commits (no renames)
git diff --name-only --no-renames SHA1 SHA2

# Show files from both commits (including deleted files)
git diff --name-only --diff-filter=ACMRT SHA1 SHA2

The --diff-filter option allows you to specify the type of changes to show:

  • A: Added
  • C: Copied
  • D: Deleted
  • M: Modified
  • R: Renamed
  • T: Type changed

Practical Examples and Use Cases

Build and Deployment Scripts

bash
# Create a deployment package with only changed files
git diff --name-only v1.0.0 HEAD | tar -czf deployment.tar.gz -T -

Code Review Preparation

bash
# List changed files for review
echo "Files changed since last release:"
git diff --name-only v1.0.0 HEAD

# Or with branch comparison
echo "Files changed in feature branch:"
git diff --name-only main feature-branch

CI/CD Pipeline Integration

bash
# In a CI script to run tests only on changed files
CHANGED_FILES=$(git diff --name-only origin/main HEAD)

for file in $CHANGED_FILES; do
    if [[ $file == *.py ]]; then
        echo "Running Python tests for: $file"
        python -m pytest "$file"
    fi
done

Documentation Updates

bash
# Check if README was modified in the last 5 commits
if git diff --name-only HEAD~5 HEAD | grep -q README.md; then
    echo "README was modified - updating documentation"
fi

Alternative Commands for Similar Tasks

git diff --name-status

This command shows the status of changed files (added, modified, deleted) along with file names:

bash
git diff --name-status SHA1 SHA2

Output example:

M   src/main.js
A   new-feature.js
D   old-feature.js

git show --name-only

To see files changed in a single commit:

bash
git show --name-only SHA1

git log --name-only

To see files changed across multiple commits in a range:

bash
git log --name-only SHA1..SHA2 --oneline

git diff-tree --no-commit-id --name-only -r

An alternative approach using git diff-tree:

bash
git diff-tree --no-commit-id --name-only -r SHA1 SHA2

This can be useful in scripts where you need just the file names without additional formatting.


Common Issues and Troubleshooting

Commits Are Reversed

If you get unexpected results, the order of commits matters:

bash
# Wrong order (may show no results)
git diff --name-only recent-commit old-commit

# Correct order
git diff --name-only old-commit recent-commit

Handling Merge Commits

For merge commits, you might need to specify the parent:

bash
git diff --name-only SHA1^ SHA2

Working with Large Repositories

In very large repositories, the command might be slow. You can optimize by:

bash
# Use abbreviated commit hashes
git diff --name-only $(git rev-parse --short SHA1) $(git rev-parse --short SHA2)

Binary Files

git diff --name-only works with binary files too, showing them as changed without attempting to show content differences.

Uncommitted Changes

If you want to see files changed in your working directory compared to a commit:

bash
git diff --name-only SHA1

This will show all files you’ve modified since that commit.

Sources

  1. Git Documentation - git diff
  2. Pro Git Book - Viewing the Commit History
  3. Git SCM - git diff Options
  4. Atlassian Git Tutorial - Comparing Commits
  5. GitHub Documentation - Viewing Commits

Conclusion

The git diff --name-only command is the essential tool for efficiently listing only the file names that changed between two commits in Git. Its simple syntax and focused output make it perfect for automation, deployment scripts, and code review preparation.

Key takeaways:

  • Use git diff --name-only SHA1 SHA2 for basic file listing between commits
  • Combine with grep and other tools for filtering and processing
  • The command handles added, modified, and deleted files seamlessly
  • Alternative commands like git show --name-only are useful for single commits
  • The output can be easily integrated into scripts and CI/CD pipelines

For most use cases, git diff --name-only provides the perfect balance of simplicity and functionality when you need to quickly identify which files were modified between specific commits in your Git history.