Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two branches yields different diffs

Tags:

git

I used Bitbucket to compare two branches. When I compared my master branch (as my source) to my uat branch (as my destination) and looked at the diff, I noticed that the list of files are totally different when I compared uat (as my source) to master (as my destination). There are a lot more files that are different when comparing master as my source. Whereas, there are only a few files that are different when comparing uat as my source. Why would the order of which I compare differ? Why would the source and destination even matter when comparing two branches?


Also, I noticed that most of the files that are listed in the diff have identical content but the only difference is the commit hash.


Here are some screenshots. I am using the BitBucket UI to compare the two branches. When letting master be my source you can see the settings folder being included in my screenshot which isn't there when I let uat1 be my source.

uat1 as my source


master as my source

like image 765
Robin Avatar asked Mar 06 '18 16:03

Robin


People also ask

How do I compare two DevOps branches?

From your web browser, open the team project for your Azure DevOps organization. In the Repos > Branches view, select the ellipsis for any branch and choose Compare branches to open the Branch compare view. In the Branch compare view, choose the two branches that you want to compare.

How do I compare the differences between two branches in GitHub?

Comparing branches is as easy as selecting the “compare to branch” option while perusing the feature branch you'd like to compare to another. The compare to branch option in GitHub Desktop is located under the “Branch” in the main menu at the top of the interface.

How do I compare changes in two branches in git?

How do I compare two different branches in my Git repository? Using git-diff you can compare two branches by viewing a diff between them, or you can use git-log to view a list of commits that comprise the difference between them. Compare two branches with git diff branch1.. branch2 .


2 Answers

The screen capture you show has a notion of source and destination branches, and has a Create pull request button.

I'm not familiar with Bitbucket GUI, but all git services that offer a Pull Request mechanism (Github, Gitlab (they're called "Merge Requests" in Gitlab), Azure Devops, ...) have a similar view :

the diff that is displayed is :

  • the list of changes that source would bring in if you merged it into destination

which is not the same as :

  • all differences between source and destination.

With a diagram :

# say your branches 'master' and 'release/onemsg-uat1' look like this :

       D--E--F <- release/onemsg-uat1  (source)
      /
--*--X--A--B <- master  (destination)
     ^
    fork point, aka "merge base"

To represent "the changes brought in by the source branch", rather than looking at the complete diff between B and F, the GUI presents you with changes starting from X :

a. if you open a Pull request to merge release/onemsg-uat1 (source) into master (destination) : the changes to review are the diff between X and F,
b. if you swap the branches, and look at what master would bring to release/onemsg-uat1 : the changes to review are the diff between X and B.


In git terminology, X is called the "merge base" of the two branches, and you can get its sha1 with git merge-base master release/onemsg-uat1.

One first way to look at those diffs from your local repo is :

# changes introduced in release/onemsg-uat1 :
git diff $(git merge-base master release/onemsg-uat1) release/onemsg-uat1

# changes introduced in master :
git diff $(git merge-base master release/onemsg-uat1) master

There is a shortcut for the above command : the three dots notation git diff a...b

# changes introduced in release/onemsg-uat1 :
git diff master...release/onemsg-uat1

# changes introduced in master :
git diff release/onemsg-uat1...master

To list only the affected files, use --name-only or --name-status :

git diff --name-only master...release/onemsg-uat1
git diff --name-status master...release/onemsg-uat1

To open this diff in a graphical diff viewer, use git difftool :

# the following command will open the differing files one at a time :
git difftool master...release/onemsg-uat1

# with the '-d' option, you will view the diff of two complete directories :
git difftool -d master...release/onemsg-uat1
like image 75
LeGEC Avatar answered Sep 19 '22 04:09

LeGEC


Edit: you're using Bitbucket's comparator, which is not git diff, and perhaps not even part of Git at all. It's not obvious what Bitbucket is doing here. It may or may not be looking at merge bases:

base=$(git merge-base $tip1 $tip2)
git diff $base $tip2

However, I thought at one point that this is what GitHub was doing for pull request displays, and then experimentation provide that it isn't what GitHub does. It may not be what Bitbucket does either (nor whether what BitBucket's web-browser-based UI does is directly related to what GitHub's does in the first place).

Since I have no idea what Bitbucket is actually comparing—though it's clearly not the two branch tips—I can't really answer the rest of this. I'll leave the original answer in, below, since it might be useful to others who find this question and really are just running git diff on the command line.


The input to git diff is (to a first approximation anyway) two commits or two snapshots: compare this snapshot with that one.

The output from git diff is a set of instructions. These instructions tell you how to change the first snapshot, in order to get the second one.

This means your initial expectation is wrong. Git is not saying: To go from an older snapshot to a newer one, add these lines here and remove those there. It's saying: To go from the left side commit/snapshot to the right-side one... If you swap the sides, you'll get inverted directions: if changing snapshot A to snapshot G requires adding a line, changing snapshot G into snapshot A will require deleting that same line.

like image 24
torek Avatar answered Sep 21 '22 04:09

torek