How can I compare and view the differences between two branches in Git?
You can compare and view differences between two branches in Git using several methods, with the most common being git diff branch1..branch2 for detailed file-by-file comparisons or git log --left-right branch1...branch2 to see commit history differences. Git also provides specialized tools like git difftool for external diff applications and GUI interfaces that visually display branch differences, making it easy to understand what has changed between branches.
Contents
- Using git diff for Branch Comparison
- Comparing with git log
- Using External Diff Tools
- GUI Tools for Branch Comparison
- Comparing Specific Files Between Branches
- Advanced Diff Options
Using git diff for Branch Comparison
The git diff command is the most fundamental tool for comparing differences between two branches. Git provides two different syntaxes for branch comparison:
Double Dot Syntax (…)
The double dot syntax shows all changes that exist in the second branch but not in the first branch:
git diff master..feature
This command compares the feature branch against the master branch and shows all changes that are present in feature but not in master. According to Baeldung on Ops, this approach is particularly useful when you want to see what changes exist in one branch relative to another.
Triple Dot Syntax (…)
The triple dot syntax compares the two branches against their common ancestor:
git diff master...feature
As Refine explains, “git diff with 3 dots compares the latest commit on your feature branch with that common ancestor commit, which is essentially the point from where you branched off to create your feature.” This is particularly useful for understanding what has changed since your feature branch diverged from the base branch.
Comparing with git log
For a more visual representation of commit differences, you can use git log with the --left-right parameter:
git log --left-right --graph --cherry-pick --oneline master...feature
This command provides a clean visualization of which commits are on which branch. Stack Overflow notes that “The git log command can also be used to view the differences between two branches” and specifically recommends this approach for a quick diff by commit.
The output will show commits from master on the left and commits from feature on the right, making it easy to see which commits are unique to each branch.
Using External Diff Tools
Git supports integration with external diff tools for more advanced comparison capabilities. You can configure and use tools like meld, kdiff3, or beyond compare for your diff operations.
Setting Up External Diff Tools
To use external diff tools, first configure Git to use them:
git config diff.tool meld
git config difftool.prompt false
Then use the git difftool command:
git difftool master..feature
According to GeeksforGeeks, “Git allows you to configure and use external diff tools like meld, kdiff3, or beyond compare.” These tools provide a more visual and interactive way to compare changes between branches.
Common External Tools
- Meld: A visual diff and merge tool that’s particularly good for comparing files side by side
- KDiff3: A powerful merge tool that can handle three-way merges and comparisons
- Beyond Compare: A commercial tool with excellent diff and merge capabilities
GUI Tools for Branch Comparison
Many Git GUI tools provide visual interfaces for comparing branches, making the process more intuitive for developers who prefer visual workflows.
Git Tower
According to Git Tower’s documentation, “You can simply select the branches in the sidebar, right-click, and select the ‘Compare…’ option from the contextual menu. Tower will then start a comparison and show the differing changes in your favorite diff tool.”
gitk
Git includes the gitk tool, which provides a visual interface for browsing your Git history and comparing branches:
gitk master..feature
phoenixNAP KB mentions that “you can launch the gitk UI tool and inspect the differences” as part of your branch comparison workflow.
Other GUI Tools
- SourceTree: Provides visual branch comparison and merging
- GitHub Desktop: Shows branch differences in a user-friendly interface
- GitKraken: Offers visual branch management and comparison
Comparing Specific Files Between Branches
Sometimes you need to compare specific files rather than entire branches. Git provides several ways to do this:
Basic File Comparison
To compare a specific file between two branches:
git diff master..feature -- README.md
Atlassian Git Tutorial explains that “To compare a specific file across branches, pass in the path of the file as the third argument to git diff.”
Showing Only File Names
If you only want to see which files have changed between branches, use the --name-only option:
git diff master..feature --name-only
Baeldung on Ops confirms that “To display only the names of files that are different between two branches, we use the ‐‐name-only option in the git diff command.”
Multiple Files
You can also compare multiple files at once:
git diff master..feature -- src/README.md config/database.yml
Advanced Diff Options
Git provides numerous options to customize diff output for different scenarios:
Ignoring Whitespace
Sometimes you want to ignore whitespace changes:
git diff master..feature --ignore-space-change git diff master..feature --ignore-all-space
OpenReplay explains that “To ignore whitespace changes when comparing branches, use the --ignore-space-change or --ignore-all-space options with git diff.”
Word-Level Diff
For seeing changes at the word level rather than line level:
git diff master..feature --word-diff=color
Numeric Diff
For showing line numbers in the output:
git diff master..feature --numstat
Summary Statistics
To get a summary of changes:
git diff master..feature --stat
This shows a summary of insertions, deletions, and modifications per file.
Sources
- How to See the Differences between two Branches? - GeeksforGeeks
- git - How can I see the differences between two branches? - Stack Overflow
- How to Compare Two Branches in Git | Learn Version Control with Git
- Find the Differences Between Two Git Branches | Baeldung on Ops
- How To Compare Two Git Branches – devconnected
- Git Diff: How to Compare Files Between Two Branches
- Git - git-diff Documentation
- git diff - Comparing Changes in Git | Refine
- How To Compare Two Git Branches | Junos Notes
- Git Diff | Atlassian Git Tutorial
Conclusion
Comparing differences between Git branches is essential for code review, debugging, and making informed merge decisions. The git diff command provides the most direct way to see file changes between branches, while git log with --left-right offers better visualization of commit history differences. For more advanced needs, external diff tools like meld and kdiff3 provide superior visual comparison capabilities, and GUI tools like Git Tower and gitk offer intuitive interfaces for branch comparison.
When working with specific files, remember to use the -- separator followed by file names, and utilize options like --name-only for quick file lists or --ignore-space-change to filter out whitespace differences. By mastering these comparison techniques, you can efficiently understand what has changed between branches and make better decisions about merging and integrating code changes.