How can I see the differences between two branches?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
To see the differences between two branches in Git, you can use the git diff command. This allows you to compare the changes in files between two branches, including additions, deletions, and modifications.
Here’s how you can do it:
1. Compare Differences Between Two Branches
Use the git diff command followed by the two branch names.
Command:
branch1: The source branch (starting point for the comparison).branch2: The target branch (the branch you want to compare against).
Example:
To compare main with feature-branch:
This shows the differences in file content between the two branches.
2. Compare Differences in Specific Files
You can narrow the comparison to specific files or directories.
Command:
Example:
This shows differences in src/app.js between the two branches.
3. See Only the Names of Changed Files
If you only want to see which files differ (not the actual changes), use the --name-only option.
Command:
Example:
Output:
4. See the Summary of Changes
To get a summary of changes (e.g., number of lines added/removed) without the full diff, use the --stat option.
Command:
Example:
Output:
5. Compare Commits Between Branches
If you want to see the commits that are different between two branches, use git log with the .. operator.
Command:
- This shows commits in
branch2that are not inbranch1.
Example:
To include a one-line summary of each commit:
6. See the Changes in Working Tree Against Another Branch
If you want to compare your current working directory with another branch:
Command:
Example:
This compares your current branch (with uncommitted changes) to feature-branch.
7. Compare the Branch History
To compare the history of two branches, use git log with --graph:
Command:
Example:
This shows a visual representation of the commit differences.
Summary Table
| Command | Purpose |
git diff branch1 branch2 | Compare file differences between two branches. |
git diff --name-only branch1 branch2 | Show only the names of files that differ. |
git diff --stat branch1 branch2 | Show a summary of changes (lines added/removed). |
git log branch1..branch2 | Show commits in branch2 that are not in branch1. |
git log branch1..branch2 --oneline | Show a concise list of commits in branch2 but not in branch1. |
git diff branch | Compare the current branch with another branch. |
Example Workflow
- Check the differences in file content:
- See which files differ:
- View a summary of changes:
By using these commands, you can efficiently compare branches in Git. 🚀

