NeuroAgent

Complete Git Diff Command for Branch Comparison

Learn how to use Git diff commands to compare files between branches. Discover syntax for comparing specific files, directories, and commits. Master branch comparison with practical examples and advanced options.

Question

How to compare files between two different Git branches

I have a script that works fine in one branch but is broken in another. I want to compare the two versions side-by-side to see what’s different. How can I use Git diff commands to compare files between branches?

To clarify: I’m not looking for a GUI compare tool (I already use Beyond Compare). I specifically need a Git diff command to compare the master version with my current branch version to identify changes. I’m not in the middle of a merge process. I want to use something like:

git diff mybranch/myfile.cs master/myfile.cs

NeuroAgent

You can compare files between two branches using git diff with the correct syntax. The command should reference the branches directly rather than creating branch-specific paths like branch/filename. Use git diff branch1 branch2 -- path/to/file to compare specific files, or git diff branch1..branch2 to see all changes between commits, or git diff branch1...branch2 to see changes from branch1 that are not yet in branch2.

Contents

Understanding Git Diff Basics

Git’s diff command is a powerful tool for comparing different versions of files. It shows you exactly what has changed between two versions, including additions, deletions, and modifications. When working with branches, git diff helps you identify what has been modified in one branch compared to another.

The fundamental syntax for comparing files between branches is:

bash
git diff <branch1> <branch2> -- <path/to/file>

This command displays the differences between the specified file in branch1 and branch2. The -- separator is important to prevent confusion between file paths and branch names, especially when your file paths might look like branch names.

Important: Git compares the version of the file as it exists at the tip (latest commit) of each branch by default.

Comparing Files Between Branches

Basic File Comparison

The most straightforward way to compare a specific file between two branches is:

bash
git diff master my-feature-branch -- src/Script.cs

This command shows you exactly what differs in src/Script.cs between master and your feature branch.

Comparing Multiple Files

To compare multiple files, simply add them to the command:

bash
git diff master my-feature-branch -- src/Script.cs src/Helper.cs

Comparing All Files in a Directory or Project

If you want to see differences across an entire directory or project:

bash
# Compare all files in a specific directory
git diff master my-feature-branch -- src/

# Compare all files in the repository
git diff master my-feature-branch

Double Dot Syntax

Git also provides a more compact syntax using double dots (..):

bash
git diff master..my-feature-branch -- src/Script.cs

This is equivalent to the previous command and shows changes that exist in my-feature-branch but not in master.

Triple Dot Syntax

For a different perspective, you can use triple dots (...):

bash
git diff master...my-feature-branch -- src/Script.cs

This shows changes from the common ancestor of both branches up to my-feature-branch. It’s useful when you want to see what your branch has added relative to where it diverged from master.


Different Comparison Scenarios

Comparing Against Current Branch

If you’re currently on a branch and want to compare against master:

bash
# Current branch is my-feature-branch
git diff master -- src/Script.cs

Comparing Specific Commits

You can compare specific commits rather than entire branches:

bash
git diff master~2 my-feature-branch~1 -- src/Script.cs

This compares the file from two commits back on master with one commit back on your feature branch.

Comparing Against a Specific Commit Hash

For precise comparisons, use commit hashes:

bash
git diff a1b2c3d4 e5f6g7h8 -- src/Script.cs

Comparing Staged Changes

If you’ve staged changes and want to see how they compare to another branch:

bash
git diff --cached master -- src/Script.cs

This compares your staged changes against the version in master.


Practical Examples and Use Cases

Scenario 1: Identifying What Changed in Your Feature Branch

bash
# See all changes in your branch compared to master
git diff master my-feature-branch

# See changes in a specific file
git diff master my-feature-branch -- src/Script.cs

# See changes in all C# files
git diff master my-feature-branch -- '*.cs'

Scenario 2: Checking Before Merging

Before merging your branch to master, you can preview the changes:

bash
# See what will be merged
git diff master..my-feature-branch

# See what will be merged in specific files only
git diff master..my-feature-branch -- 'src/**/*.cs'

Scenario 3: Comparing with Another Developer’s Branch

bash
git diff master colleague-branch -- src/Script.cs

Scenario 4: Creating a Patch File

If you want to save the differences to apply elsewhere:

bash
git diff master my-feature-branch -- src/Script.cs > script.patch

Then you can apply this patch to another repository or branch:

bash
git apply script.patch

Scenario 5: Interactive Comparison

For a more interactive experience, you can use git difftool:

bash
git difftool master my-feature-branch -- src/Script.cs

This will open your configured diff tool (like Beyond Compare) for side-by-side comparison.


Advanced Options and Output Formatting

Color Output

Git colors the diff output by default, but you can control this:

bash
git diff --color=always master my-feature-branch -- src/Script.cs
git diff --color=never master my-feature-branch -- src/Script.cs

Unified Diff Format

Control the amount of context shown:

bash
# Show 3 lines of context (default)
git diff -U3 master my-feature-branch -- src/Script.cs

# Show 5 lines of context
git diff -U5 master my-feature-branch -- src/Script.cs

# Show no context (only changed lines)
git diff -U0 master my-feature-branch -- src/Script.cs

Word-Level Diff

See changes word by word instead of line by line:

bash
git diff --word-diff=plain master my-feature-branch -- src/Script.cs

Summary Statistics

Get a quick summary of changes instead of the full diff:

bash
git diff --stat master my-feature-branch -- src/Script.cs

Binary Diff

For binary files, use the --binary flag:

bash
git diff --binary master my-feature-branch -- assets/image.png

Ignore Whitespace Changes

Sometimes you want to ignore whitespace changes:

bash
git diff --ignore-space-change master my-feature-branch -- src/Script.cs
git diff --ignore-all-space master my-feature-branch -- src/Script.cs
git diff --ignore-blank-lines master my-feature-branch -- src/Script.cs

Filtering by Change Type

Show only additions, deletions, or modifications:

bash
# Show only additions
git diff --diff-filter=A master my-feature-branch -- src/Script.cs

# Show only deletions
git diff --diff-filter=D master my-feature-branch -- src/Script.cs

# Show only modifications (not additions or deletions)
git diff --diff-filter=M master my-feature-branch -- src/Script.cs

Output to Different Formats

Git can output diffs in various formats:

bash
# Output as HTML
git diff --html master my-feature-branch -- src/Script.cs > diff.html

# Output as plain text with line numbers
git diff --numstat master my-feature-branch -- src/Script.cs

Conclusion

To effectively compare files between Git branches, remember these key points:

  1. The correct syntax is git diff <branch1> <branch2> -- <path/to/file>, not git diff branch1/filename branch2/filename as you initially attempted.

  2. Use double dots (..) to see changes that exist in the second branch but not the first, and triple dots (...) to see changes relative to the common ancestor.

  3. Always include the -- separator when specifying file paths to avoid confusion with branch names.

  4. Combine with various options like --stat for quick summaries, --word-diff for detailed changes, or --color for better readability.

  5. For complex comparisons, use git difftool to integrate with your preferred GUI diff tool like Beyond Compare.

By mastering these Git diff commands, you can efficiently identify what changed between branches, debug issues in your script, and make informed decisions about which changes to merge or discard. The ability to precisely compare files and directories between branches is an essential skill for effective Git workflow management.

Sources

  1. Official Git Documentation - git diff
  2. Pro Git Book - Viewing the Commit History
  3. Atlassian Git Tutorial - Comparing Branches
  4. GitHub Help - Comparing branches
  5. Git Diff Examples and Tips