branches
coding
git diff
git

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:

bash
git diff branch1 branch2
  • 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:

bash
git diff main 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:

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

Example:

bash
git diff main feature-branch -- src/app.js

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:

bash
git diff --name-only branch1 branch2

Example:

bash
git diff --name-only main feature-branch

Output:

 
src/app.js
src/utils.js

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:

bash
git diff --stat branch1 branch2

Example:

bash
git diff --stat main feature-branch

Output:

 
 src/app.js      |  12 +++++-----
 src/utils.js    |  25 +++++++++++++++-----
 2 files changed, 27 insertions(+), 10 deletions(-)

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:

bash
git log branch1..branch2
  • This shows commits in branch2 that are not in branch1.

Example:

bash
git log main..feature-branch

To include a one-line summary of each commit:

bash
git log main..feature-branch --oneline

6. See the Changes in Working Tree Against Another Branch

If you want to compare your current working directory with another branch:

Command:

bash
git diff branch

Example:

bash
git diff feature-branch

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:

bash
git log branch1..branch2 --graph --oneline

Example:

bash
git log main..feature-branch --graph --oneline

This shows a visual representation of the commit differences.


Summary Table

CommandPurpose
git diff branch1 branch2Compare file differences between two branches.
git diff --name-only branch1 branch2Show only the names of files that differ.
git diff --stat branch1 branch2Show a summary of changes (lines added/removed).
git log branch1..branch2Show commits in branch2 that are not in branch1.
git log branch1..branch2 --onelineShow a concise list of commits in branch2 but not in branch1.
git diff branchCompare the current branch with another branch.

Example Workflow

  1. Check the differences in file content:
bash
 git diff main feature-branch
  1. See which files differ:
bash
 git diff --name-only main feature-branch
  1. View a summary of changes:
bash
 git diff --stat main feature-branch

By using these commands, you can efficiently compare branches in Git. 🚀


Course illustration
Course illustration

All Rights Reserved.