isuruanu
isuruanu

Reputation: 33013

How can I see the differences between two branches?

How can I see the differences between branches branch_1 and branch_2?

Upvotes: 3294

Views: 2609497

Answers (12)

Lazy Badger
Lazy Badger

Reputation: 97355

Use git diff.

git diff [<options>] <commit>..​<commit> [--] [<path>…​]

<commit> is a branch name, a commit hash, or a shorthand symbolic reference.

Examples:

git diff abc123..def567

git diff HEAD..origin/master

That will produce the diff between the tips of the two branches. If you'd prefer to find the changes that occurred in the common ancestor since the branch was started off, you can use three dots instead of two:

git diff <commit>...<commit>

To check which files differ, not how the content differs, use --name-only:

git diff --name-only <commit>..​<commit>

Note that in the <commit>..<commit> (two dot) syntax, the dots are optional; the following is synonymous:

git diff commit1 commit2

Upvotes: 4027

F1Linux
F1Linux

Reputation: 4383

GitHub Branches Showing out-of-sync When They Weren't:

When Googling to understand why GitHub kept showing my branches out-of-sync when I was 200% they weren't, this question kept appearing at the top of the results pile. So I'll provide what I've learned to help other folks with the same question who land here.

SOLUTION:

GitHub does three-dot comparisons (digested below) BY DEFAULT- NOT TWO dot:

The two-dot comparison shows the difference between the latest state of the base branch (for example, main) and the most recent version of the topic branch.

Since the three-dot comparison compares with the merge base, it is focusing on "what a pull request introduces".

How to toggle between 2-dot and 3-dot branch comparisons on GitHub:

The URL of the diff'ed branches is presented as below with THREE dots:

https://github.com/yourAcct/yourRepo/compare/develop...main

Just backspace one of the three dots and GitHub will return the results of a TWO dot diff:

https://github.com/yourAcct/yourRepo/compare/develop..main

If your branches are truly in-sync, GitHub won't report any changes in the TWO-dot comparison.

Conclusion:

GitHub doesn't expressly state how they are calculating the differences and I believe many like myself will see a message in GitHub that "This branch is X commits behind" and think: "I must have made a boo-boo..." Well, you didn't.

Not believing it was possible that GitHub was gaslighting me, I needed to prove the state of sync. It was only after this testing I knew something was wrong and continued investigating.

Tests to prove my branches were sync'ed:

In all the tests below, NO changes were reported:

Test 1: LOCAL branches were in sync with each other:

git diff develop..main

Test 2: REMOTE branches in sync with each other:

git diff origin/develop..origin/main

Test 3: LOCAL branches in sync with their REMOTE counterparts:

git diff develop..origin/develop

git diff main...origin/main

Test 4: LOCAL branches were in sync with the other branch:

git diff develop...origin/main
git diff main...origin/develop

Only after validating my branches were in fact sync'ed could I consider the possibility the status returned by GitHub might not be what it appeared to be...

Upvotes: 3

VyTre
VyTre

Reputation: 156

For AzureDevOps users:

  • Go to Repos / Branches

branches list

  • On the right side, click on the sub menu next a branch

menu

  • Select branches to compare

enter image description here

Upvotes: 3

Eamonn Kenny
Eamonn Kenny

Reputation: 2062

I've tried most of the responses above. None of them work anymore because revisions and branches are completely separated. The responses with the lower rating are actually much closer to the correct answer.

The easiest solution is:

 git diff origin/branch1 origin/branch2

And the beauty of this is that this works even if you are on origin/main (in github/gogs) or origin/master (in gitlab).

Upvotes: 4

Sadekur Rahaman
Sadekur Rahaman

Reputation: 751

You can simply show difference using git diff b1...b2, or you can show commit difference using git log b1..b2.

You can see the commit difference in a nice graphical way using git log --oneline --graph --decorate --abbrev-commit b1..b2.

Upvotes: 32

poleguy
poleguy

Reputation: 585

Sometimes it's nice to see the diff as a tree...

git difftool --dir-diff branch..otherBranch

or to compare a remote branch to the local workspace...

git difftool --dir-diff origin/branch .

For example, when bitbucket decides for performance reasons it will only show you a "three way merge" diff rather than the actual complete differences between the two branches you've selected.

This will show the diff as a tree in the tool you've selected. e.g. in meld.

Inspired by @GregRundlett comment.

Upvotes: 10

Ritik Banger
Ritik Banger

Reputation: 2758

There are two ways to see the differences between two branches.The modifications that have been made to the files in each branch will be shown by these commands.

  1. Use the git diff command to view the differences between two branches in a Git repository.

    git diff branch1 branch2 will show all the differences.

    If you wish to compare a specific file between the two branches, you can use this command as:

    git diff branch1 branch2 path/to/file

  2. The git log command can also be used to view the differences between two branches. Run the git log command with the —left-right parameter and the two branches you wish to compare like this:

    git log --left-right branch1...branch2

Upvotes: 8

Liran H
Liran H

Reputation: 10609

When on the feature branch, merge your target branch and then run a diff against it. For example, if you want to see what changes your feature branch add to master, do the following:

// Fetch from all remotes
git fetch

// Check out your feature branch
git checkout feature

// Merge origin/master to your branch
git merge origin/master

// Compare to origin/master
git diff origin/master

Upvotes: 1

Nagibaba
Nagibaba

Reputation: 5398

git diff master..develop

Options:

  • Add --name-only to only see the names of the files.
  • Add -- folderOrFileName at the end to see the changes of specific files or folders.
  • To compare the local branch with the remote one, then run git fetch --all to fetch all remote branches, and run:
    git diff --name-only [branchName]..origin/[branchName]
    
    Example: git diff --name-only develop..origin/develop.

Upvotes: 83

Neeraj Kumar
Neeraj Kumar

Reputation: 7559

Go to a branch (e.g. main), then run diff against another branch (e.g. branch2):

git checkout main
git diff branch2

Upvotes: 196

Anuj
Anuj

Reputation: 11

In Eclipse(J2EE version) , open "Window --> Show view --> Git Repository". if you have checked out 2 local git branches for examples then you will have bunch of branches in Local section. select any 2 git local branches and do " right click and select "Compare with each other in Tree menu".

Open view "Git Tree Compare" and u will be able to see side by side diff for all files.

Upvotes: 1

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24630

There are many different ways to compare branches, and it's depend on the specific use case you need.

Many times you want to compare because something broken and you want to see what has been changes, then fix it, and see again what changed before commiting.

Personally when I want to see the diff what I like to do:

git checkout branch_1 # checkout the oldest branch
git checkout -b compare-branch # create a new branch
git merge --no-commit --squash branch_2 # put files from the new branch in the working folder
git status # see file names that changes
git diff # see the content that changed.

Using this solution you will see the diff, you can also see only the file names using git status, and the most important part you will be able to execute branch_2 while seeing the diff (branch_2 is on the working tree). If something had broken you can editing the files and fix it. Anytime, you can type again git status or git diff to see the diff from the new edit to branch_a.

Upvotes: 16

Related Questions