git: merge changes to a branch on a different remote

It is quite common to have have several remote repositories, let's see how to merge a change to a branch on a remote that collides with one of our local branches.

Let's assume you have been working on a local branch called CHG-1234 that you want to merge to a different remote repository on the master branch. First off, we will have to add the remote:

$ git remote add salta

Then we need to download objects and refs from this repository

$ git fetch salta

To be able to checkout the master branch from the remote repository we will have to assign a different name for the local branch we will be working on, for example master_salta

$ git checkout -b master_salta --track salta/master
Branch 'master_salta' set up to track remote branch 'master' from 'salta'.
Switched to a new branch 'master_salta'

Since it automatically switches to the new local branch, we can merge the commits from our aforementioned CHG-1234 branch:

$ git merge CHG-1234

Finally to push the changes back to the master branch on the remote repository we will have to explicitly say to which branch we want to push the changes using HEAD:master:

$ git push salta HEAD:master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 12 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.14 KiB | 1.14 MiB/s, done.
Total 7 (delta 6), reused 0 (delta 0)
   1ddf1b1..c1c1c1c  HEAD -> master

Posted on 28/12/2020