NeuroAgent

How to See Changed Files Between Git Branches

Discover efficient ways to compare Git branches and identify changed files. Complete guide with git diff commands, practical examples, and best practices for merge preparation.

Question

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..branch
  • git log master..branch
  • git 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?

NeuroAgent

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:

bash
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

git diff --name-only

The most straightforward solution to your problem is:

bash
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:

bash
git diff --name-status master..branch

This shows:

  • M - Modified files
  • A - Added files
  • D - Deleted files
  • R - Renamed files
  • C - Copied files

Three-dot vs Two-dot syntax

  • git diff master..branch - Shows changes in branch since it diverged from master
  • git diff master...branch - Shows changes unique to branch plus changes in master that branch doesn’t have
bash
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:

bash
git diff --name-only master..branch | sort -u

Alternative Unique Methods

bash
# 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:

bash
git diff --name-status master..branch | grep '^M' | cut -f2

To get only added files:

bash
git diff --name-status master..branch | grep '^A' | cut -f2

Advanced Comparison Options

Count of Changed Files

Get just the count of changed files:

bash
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:

bash
git diff --name-only master..branch | xargs ls -1 2>/dev/null

Regex Filtering

Filter files by extension or pattern:

bash
# 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:

bash
git log --name-only master..branch | grep -v '^$' | sort -u

Visualizing Changes

Summary Statistics

Get a summary of changes:

bash
git diff --stat master..branch

ASCII Graph

See a visual representation of changes:

bash
git log --oneline --graph --name-only master..branch

Practical Examples

Example 1: Basic Comparison

bash
# List all changed files between master and feature-branch
git diff --name-only master..feature-branch

Example 2: Unique Modified Files

bash
# Get unique list of modified files (no duplicates)
git diff --name-only master..feature-branch | sort -u

Example 3: Prepare for Merge

bash
# 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

bash
#!/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

  1. Use --name-only - This is the most efficient way to get just filenames
  2. Sort and unique - Use | sort -u to eliminate duplicates from multiple commits
  3. Check file existence - Use | xargs ls -1 2>/dev/null to see only files that exist
  4. Filter by status - Use --name-status to understand what type of changes occurred
  5. Use three-dot syntax when appropriate - git diff master...branch shows changes unique to each branch

The most efficient solution to your current approach is:

bash
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

  1. Official Git Documentation - git diff
  2. Atlassian Git Tutorial - Comparing branches
  3. Git Pro Book - Viewing the history
  4. 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..branch for a quick list of changed files
  • For unique results: git diff --name-only master..branch | sort -u to eliminate duplicates
  • For detailed status: git diff --name-status master..branch to see modification types
  • For existing files only: Combine with xargs ls -1 2>/dev/null to 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.