How can I see which files have changed between two Git branches?
I want to merge two branches that have been separated for a while and need to identify which files have been modified. I’ve tried the following Git commands to compare branches:
git diff master..branchgit log master..branchgit shortlog master..branch
Is there a command similar to git status master..branch that would only show the files that are different between the two branches?
Currently, I’m using:
git diff master..branch | grep "^diff"
But this shows repeated entries if a file was modified multiple times. Is there a more efficient way to accomplish this?
You can see which files have changed between two Git branches using several efficient commands. The most direct approach is to use git diff with the --name-only flag, which shows only the filenames that have been modified.
The simplest command to get a unique list of changed files is:
git diff --name-only master..branch
This will display only the filenames that differ between the branches, without any of the actual diff content.
Contents
- Basic File Comparison Commands
- Getting Unique Lists of Changed Files
- Advanced Comparison Options
- Visualizing Changes
- Practical Examples
- Best Practices
Basic File Comparison Commands
git diff --name-only
The most straightforward solution to your problem is:
git diff --name-only master..branch
This command lists only the filenames that have been modified, added, or deleted between the two branches.
git diff --name-status
For more detailed information about the type of changes:
git diff --name-status master..branch
This shows:
M- Modified filesA- Added filesD- Deleted filesR- Renamed filesC- Copied files
Three-dot vs Two-dot syntax
git diff master..branch- Shows changes inbranchsince it diverged frommastergit diff master...branch- Shows changes unique tobranchplus changes inmasterthatbranchdoesn’t have
git diff --name-only master...branch
Getting Unique Lists of Changed Files
Simple Unique List
If you’re seeing duplicate entries (when a file was modified multiple times), use sort -u:
git diff --name-only master..branch | sort -u
Alternative Unique Methods
# Using awk to remove duplicates
git diff --name-only master..branch | awk '!x[$0]++'
# Using sort with unique flag
git diff --name-only master..branch | sort -u
# Using uniq command (after sorting)
git diff --name-only master..branch | sort | uniq
Filter by File Status
To get only modified files:
git diff --name-status master..branch | grep '^M' | cut -f2
To get only added files:
git diff --name-status master..branch | grep '^A' | cut -f2
Advanced Comparison Options
Count of Changed Files
Get just the count of changed files:
git diff --name-only master..branch | wc -l
Files That Actually Exist
Sometimes you might get references to deleted files. To see only files that currently exist:
git diff --name-only master..branch | xargs ls -1 2>/dev/null
Regex Filtering
Filter files by extension or pattern:
# Only .js files
git diff --name-only master..branch | grep '\.js$'
# Only files in src directory
git diff --name-only master..branch | grep '^src/'
Using Git Log
You can also use git log with filename filtering:
git log --name-only master..branch | grep -v '^$' | sort -u
Visualizing Changes
Summary Statistics
Get a summary of changes:
git diff --stat master..branch
ASCII Graph
See a visual representation of changes:
git log --oneline --graph --name-only master..branch
Practical Examples
Example 1: Basic Comparison
# List all changed files between master and feature-branch
git diff --name-only master..feature-branch
Example 2: Unique Modified Files
# Get unique list of modified files (no duplicates)
git diff --name-only master..feature-branch | sort -u
Example 3: Prepare for Merge
# List all changed files, check which ones you need to review
git diff --name-status master..feature-branch
# Get only files that were modified (not added or deleted)
git diff --name-only master..feature-branch | xargs ls -1 2>/dev/null
Example 4: Create a Script
#!/bin/bash
BRANCH1=$1
BRANCH2=$2
echo "Files changed between $BRANCH1 and $BRANCH2:"
git diff --name-only $BRANCH1..$BRANCH2 | sort -u
echo -e "\nTotal count: $(git diff --name-only $BRANCH1..$BRANCH2 | wc -l) files"
Best Practices
- Use
--name-only- This is the most efficient way to get just filenames - Sort and unique - Use
| sort -uto eliminate duplicates from multiple commits - Check file existence - Use
| xargs ls -1 2>/dev/nullto see only files that exist - Filter by status - Use
--name-statusto understand what type of changes occurred - Use three-dot syntax when appropriate -
git diff master...branchshows changes unique to each branch
The most efficient solution to your current approach is:
git diff --name-only master..branch | sort -u
This gives you the clean, unique list of changed files without the overhead of parsing diff output with grep.
Sources
- Official Git Documentation - git diff
- Atlassian Git Tutorial - Comparing branches
- Git Pro Book - Viewing the history
- Stack Overflow - Get unique list of changed files between branches
Conclusion
To summarize the most effective ways to see which files have changed between two Git branches:
- Simplest solution:
git diff --name-only master..branchfor a quick list of changed files - For unique results:
git diff --name-only master..branch | sort -uto eliminate duplicates - For detailed status:
git diff --name-status master..branchto see modification types - For existing files only: Combine with
xargs ls -1 2>/dev/nullto filter out deleted files
Your current approach using git diff master..branch | grep "^diff" works but is inefficient compared to the built-in --name-only flag. The git diff command is specifically designed for this use case and will perform better while being more readable.
For your merge preparation, I recommend starting with git diff --name-status master..branch to understand both what files changed and how they changed, then use the filtering techniques shown above to focus on the specific information you need.