55

When I do a Github comparison view between master and another branch A, Github seems to be comparing the HEAD version of A with an older, non head version of master.

I looked into it, and from what I can tell it sounds like Github is comparing branch A against a common ancestor of master. It's not actually comparing it to what's currently at the HEAD of master.

Is there a way to diff the HEAD of master against the HEAD of Branch A in Github?

If not, why not? This seems like a feature every developer would want to be able to do. Or is there some process that should be done that I'm perhaps missing? I'd like to be able to create a Pull Request directly from one of these diffs.

EDIT: Here's an example repo that shows the problem

https://github.com/bradparks/test_github_diff_view

  • I created a new repo that contains a single file, README.md
  • I set this file to contain a single line v100.
  • I then branched master into a new branch A, and changed the line to `v200'
  • I then changed the value in the master branch to 'v300', and then compared the 2 branches using the compare link

    https://github.com/bradparks/test_github_diff_view/compare/A

    and I see the following unexpected result. Why isn't it diffing against v300 instead of v100?

    enter image description here

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
  • Can you please provide a query to the comparison where you think there is an error. (didn't dv btw) – Willem Van Onsem Sep 14 '15 at 17:52
  • @BradPranks: I'm talking about the url to get the comparison view (a url with query, something of the form: `github.com/foo/bar?arg=val&oth=val2`). – Willem Van Onsem Sep 14 '15 at 17:57
  • 1
    @CommuSoft - thanks for the feedback - I've added a sample repo showing the problem, and added it to the question above. – Brad Parks Sep 14 '15 at 18:15
  • 1
    GitHub compares against the last time you merged from the parent branch. Which if you never merged master into your branch will be the point in time you created the branch. – Powerlord Sep 14 '15 at 18:17
  • Hmm... Interesting... so to merge branch `A` into `master`, I first have to merge `master` into branch `A` ? My branch `A` is a feature branch. – Brad Parks Sep 14 '15 at 18:23

3 Answers3

43

Is there a way to diff the HEAD of master against the HEAD of Branch A in Github?

Sept 2018: yes: GitHub now explicitly supports "Three-dot and two-dot Git diff comparisons". See an example here.
keisuke mentions in the comments a similar initiative on Bitbucket.

Original answer 2015:

No: as mentioned in "GitHub compare view for current versions of branches", GitHub only supports the triple dots (...) range shortcut specification.
That is:

This form is to view the changes on the branch containing and up to the second , starting at a common ancestor of both.

"git diff A...B" is equivalent to "git diff $(git-merge-base A B) B".
You can omit any one of , which has the same effect as using HEAD instead.

The official GitHub help mentions this feature as:

The most common use of Compare is to compare branches, such as when you're starting a new Pull Request.

In that scenario, the PR branch starts from master (or should be rebased on top of master anyway), which means master HEAD is the base between master and the PR branch.

But when the two branches have forked, the comparison is no longer between HEADs, but between one common ancestor and one HEAD: git diff $(git-merge-base master B) B.

Note: even if you were to specify two SHA1 directly, as it is clearly documented in "Comparing commits", that would still do a git diff $(git-merge-base A B) B.
That would not do a diff directly between the two commits.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks for the feedback! can you clarify what you mean by ***"In that scenario, the PR branch starts from master (or should be rebased on top of master anyway)"*** - Do you mean I should be merging master into the PR branch? Which sounds kind of like what Powerlord was saying - ***"Which if you never merged master into your branch will be the point in time you created the branch.***" But as I mentioned it seems like I have to merge master into branch A before I can merge A into master? – Brad Parks Sep 17 '15 at 11:50
  • 1
    @BradParks see my previous answers http://stackoverflow.com/a/24978606/6309 and http://stackoverflow.com/a/14681796/6309 – VonC Sep 17 '15 at 11:52
  • 6
    I tried this and it worked great. It's like I had to resolve all merge conflicts before actually merging, at least in my view of things ;-) ***But doesn't this make the "compare" view supplied by Github almost useless?*** I know they're constrained to some extent by what git's workflows are, but it still seems to me like that compare view is very misleading to 99% of people. I think it'd almost be better if it didn't even have the feature, given how broken it seems. – Brad Parks Sep 17 '15 at 12:32
  • 1
    @BradParks the goal is indeed to make sure the conflicts are resolved locally, before any PR is proposed: that way the merge will be fast forward. Since the PR branch is on top of the master, the GitHub diff will show the diff between master HEAD and the PR branch HEAD. – VonC Sep 17 '15 at 12:37
  • 1
    @BradParks I have updated my answer to explain why leoger's answer cannot work: you will always diff with the common ancestor, not HEAD of another branch. – VonC Sep 19 '15 at 10:24
  • 1
    And to be 100% clear the only way to make this work is to [use the process detailed here](http://stackoverflow.com/a/24978606/26510) by @VonC, which involves rebasing the feature branch, `A`, before doing the pull request. – Brad Parks Sep 23 '15 at 17:29
  • 1
    GitHub recently implemented two-dots-diff feature: https://blog.github.com/changelog/2018-09-18-two-dot-comparison/ , https://help.github.com/articles/about-comparing-branches-in-pull-requests/#three-dot-and-two-dot-git-diff-comparisons . By the way BitBucket's default pull requests are compared in two-dots-diff, which is more user-friendly as you say. See: https://developer.atlassian.com/blog/2015/01/a-better-pull-request/ – keisuke Oct 03 '18 at 08:48
  • 1
    @keisuke Thank you. I actually mentioned it in https://stackoverflow.com/a/52487331/6309, but forgot to report it here. I have edited the answer accordingly, and included your comment for more visibility. – VonC Oct 03 '18 at 08:55
1

Is there a way to diff the HEAD of master against the HEAD of Branch A in Github?

[EDIT: Nope, I was wrong. I managed had a URL which looked like it would be doing the right thing and the diff on the page looked right, but that was an accident. If you really want to know more, check the history of this answer.]

leoger
  • 1,074
  • 10
  • 16
  • hey... thanks for the feedback! I tried it and it doesnt seem to work for me... Everything after the # mark isn't sent to the server, and I tried changing the url to lots of different things and they all give the same result. Just the last commit to master, which makes it look like it kind of works. e.g. https://github.com/bradparks/test_github_diff_view/commit/2c6f822f28#PART_HERE_NOT_USED-HASH_NOT_BEING_USED – Brad Parks Sep 18 '15 at 12:16
  • I'm a little confused. If you click the link I provided to your test_github_diff_vew repo, do you not see the diff you want? The diff I see shows "-v100" and "+v300". Is Github showing you and me two different pages for the same URL? – leoger Sep 18 '15 at 21:51
  • i do see the diff I want when I click on the link that way, which at first I thought was great. But then I realized that it's just showing the last commit to the master branch. It doesn't matter what you put at the end, it always just shows the last commit to that branch. So it's not a diff, it's just a link to a single commit to a single branch. For example, https://github.com/bradparks/test_github_diff_view/commit/master – Brad Parks Sep 19 '15 at 00:49
  • Huh, you're right! Sorry for not verifying my answer more carefully. – leoger Sep 20 '15 at 23:47
1

Just another thought. If you want to create a pull request from the diff between your work and the head of the master branch (as per o.p.), then what you need to do is to firstly rebase your own branch off the master branch git rebase <master> and then make the pull request.

But you typically don't need to do this, Git is very clever with merges and will normally be able to add your work in to the master branch as you want it added, even if you don't rebase first.

MikeBeaton
  • 3,314
  • 4
  • 36
  • 45