Simon H
Simon H

Reputation: 21005

Git : apply the diff between two branches directly to master

I have a branch b1, and another b2 which builds on b1. I can see now that b2 is not dependent on b1, and I'd like to create a PR based on the diff b2 - b1 and put that up for merging into master first.

I can see the diff on github if I set it up to compare with b1 (see image), but have no idea how to extract that as a separate PR.

diff summary

I guess I need to save the diff to a file and then create a branch from master to apply that to.

Per the request @LeGEC, this is the result

* ec82b57a7 (origin/sh/datatree-queries, sh/datatree-queries) tidier
* ddcb05459 tidier
* 6ebb2199f tidier
* 10e3a5e0f clipboard items
* ff08a1310 wip - UI
* 0169a4dbe insert entire query ast
| * 4e8a00c1a (HEAD -> sh/datatree-fx, origin/sh/datatree-fx) revert
|/  
o 63d147bc7 Merge remote-tracking branch 'origin/master' into sh/datatree-fx

Upvotes: 1

Views: 961

Answers (2)

LeGEC
LeGEC

Reputation: 51780

You can use git rebase to take the commits specific to b2 and replay them on top of master :

# the following command says :
#  - apply on master
#  - the list of commits starting from 'b1' ('b1' excluded)
#  - up to 'b2' ('b2' included)
git rebase --onto master b1 b2

then open a PR for b2


[edit]

In your case : b1 has on extra commit, which explains the difference in behavior :

  • git rebase --onto master b1 b2 would only select the 6 commits on the left side of the graph,
  • git diff b1 b2, on the other hand, takes also into account the differences in content with the revert commit

In your situation : creating the patch and applying it is a valid way to reach the result you want.

Upvotes: 2

Simon H
Simon H

Reputation: 21005

I have since found How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?

What worked was to cd to the root of the repo and then

git diff sh/datatree-fx sh/datatree-queries > mydiff.diff
git apply mydiff.diff

Upvotes: 1

Related Questions