How Can I See the Differences between Two Branches?

Better Stack Team
Updated on June 24, 2024

To see the differences between two branches in Git, you can use the git diff command followed by the names of the branches you want to compare. Here's how you can do it:

 
git diff <branch1> <branch2>

Replace <branch1> and <branch2> with the names of the branches you want to compare. This command will show the differences between the two branches.

For example, to see the differences between the main branch and a feature branch named feature/new-feature, you would run:

 
git diff main feature/new-feature

Viewing Differences with Color and Context:

You can also add options to git diff to display the differences with color highlighting and context lines. For example:

 
git diff --color-words --no-index <branch1> <branch2>

This command will display word-level differences with color highlighting.

Note:

  • If you want to compare the current branch with another branch, you can omit one of the branch names in the git diff command. For example, git diff main will compare the current branch with the main branch.
  • The git diff command can also be used to compare commits, tags, or specific files. You can specify commit hashes, tag names, or file paths instead of branch names.