Have you ever needed to share code between different branches? More specifically across Pull Requests?
And what if those Pull Requests were started or aimed at different branches? (maybe master and.. releaseXX, making it difficult if those branches are a bit diverging at the moment)
One option would be, if changes are simple, copy changes by hand.
A better way would be saving a patch/diff file and applying it in the other branch.
The problem is that it won’t retain commits ownership.
But there is a combination of commands to use that can:
– Pass each commit
– Retain ownership of each change
– Have an almost similar history in each branch regardless of diverging base branch
Having the syntax:
git format-patch [BASE_BRANCH_NAME]
Let’s create one patch file with all changes:
$ git format-patch master --stdout > big-change.patch
We can apply the patch with (in a different branch or working directory):
$ cat big-change.patch | git am
What if we have errors during git-am..?
We should try a 3-way merge with:
git am --3way
It’s likely that CONFLICTS will happen that we’ll be able to resolve with git mergetool.
After solving all them it can continue with the next patch (each commit inside our single file):
git am --resolved