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
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
- Comparing Files Between Branches
- Different Comparison Scenarios
- Practical Examples and Use Cases
- Advanced Options and Output Formatting
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:
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:
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:
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:
# 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 (..):
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 (...):
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:
# Current branch is my-feature-branch
git diff master -- src/Script.cs
Comparing Specific Commits
You can compare specific commits rather than entire branches:
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:
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:
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
# 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:
# 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
git diff master colleague-branch -- src/Script.cs
Scenario 4: Creating a Patch File
If you want to save the differences to apply elsewhere:
git diff master my-feature-branch -- src/Script.cs > script.patch
Then you can apply this patch to another repository or branch:
git apply script.patch
Scenario 5: Interactive Comparison
For a more interactive experience, you can use git difftool:
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:
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:
# 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:
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:
git diff --stat master my-feature-branch -- src/Script.cs
Binary Diff
For binary files, use the --binary flag:
git diff --binary master my-feature-branch -- assets/image.png
Ignore Whitespace Changes
Sometimes you want to ignore whitespace changes:
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:
# 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:
# 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:
-
The correct syntax is
git diff <branch1> <branch2> -- <path/to/file>, notgit diff branch1/filename branch2/filenameas you initially attempted. -
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. -
Always include the
--separator when specifying file paths to avoid confusion with branch names. -
Combine with various options like
--statfor quick summaries,--word-difffor detailed changes, or--colorfor better readability. -
For complex comparisons, use
git difftoolto 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.