git: merge changes to a branch on a different remote

2 min read

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 git@github.com:SaltaIT/eyp-systemd.git

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)
To git@github.com:SaltaIT/eyp-systemd.git
   1ddf1b1..c1c1c1c  HEAD -> master

Posted on 28/12/2020

Categories