2

How to do diff between branches to get what the Merge diff will look like

consider this graph

*   master
|\
| * b1
|\ 
| * b2

if im on b2, and the person on b1 beat me and merged into master,

if i do

project>b2 $ git diff origin/master

the diff would include b1, which would not be included in the PR, how do I reproduce this in command line.

user2167582
  • 5,986
  • 13
  • 64
  • 121
  • Possible duplicate of [How can I preview a merge in git?](http://stackoverflow.com/questions/5817579/how-can-i-preview-a-merge-in-git) – max630 Nov 20 '16 at 16:57

3 Answers3

1

I believe that git merge-tree will do it. It doesn't perform a merge but instead outputs a diff which represents the merge.

It takes three arguments: git merge-tree <base-tree> <branch1> <branch2>

I find it easier to read when piping its output into colordiff and then a pager. (I use less with -r to handle the colors.)

git merge-tree $(git merge-base HEAD b2) HEAD b2 | colordiff | less -r
JellicleCat
  • 28,480
  • 24
  • 109
  • 162
0

You can update origin/master with git fetch origin. This will update your remote branches without changing anything else.

After that, you can rebase your b2 work onto origin/master with git rebase origin/master. Now b2 will be based on the latest work including b1. Then git diff origin/master will reflect the addition of b1.

Then you can update the PR with git push --force.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • that sounds very practical and straight forward, and it sounds like you have posed or have been posed to this problem. But is there a command i can do that grants that for me? and what if i want to get a preview without actually changing content on any branch. I dont htink it would take a lot of work right, all we need to check is for commits unknown to the master branchs and merge those commits. – user2167582 Nov 18 '16 at 17:58
  • @user2167582 You don't have to do the rebase. Just `git fetch origin` to get the updated `origin/master` and then diff. But if your purpose is to submit `b2` as a pull request, you should update it on top of the latest `origin/master` (ie. rebase) and make sure it still works. – Schwern Nov 18 '16 at 21:28
0

With Git 2.38+ (Q3 2022), you can use quickly check if there will be conflicts between your feature branch and the PR target branch, using git merge-tree --write-tree:

git merge-tree --write-tree --no-messages --name-only branch1 branch2

With a GUI like GitHub Desktop (for remote GitHub repositories), you actually can, since March 2023:

GitHub Desktop 3.2: Preview your pull request

In GitHub Desktop 3.1, we introduced viewing the diff of changes across multiple commits.
This allows you to be certain there are no unintended changes in the group of commits you are about to push.

Taking that feature to the next level, GitHub Desktop 3.2 allows you to “Preview your Pull Request”– see a diff of all the changes being introduced by your feature branch into your repository’s default branch.

GitHub Desktop helps you feel confident in your Git workflows, and now we want to help you feel confident in your GitHub workflows as well.

Preview your pull request

If you find yourself apprehensive to push your changes up to GitHub.com and open a pull request, you will like the confidence boost reviewing your pull request locally will give you.
Have you ever submitted a pull request only to find you’ve accidentally left in a debugger statement, requiring you to return back to your local environment, remove the debugger, commit, and push up the change? This can be annoying, time consuming, and maybe even a little embarrassing.

Now with the “Preview Pull Request” feature, you can see the diff of all the changes brought in from all the commits on your feature branch before opening your pull request. It lets you do that double-checking before leaving your local development environment.

The pull request preview open showing a file change with a debugger in it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250