Monday, December 23, 2013

Checking in the same change on multiple remote branches

We have a situation where we need to commit the same change on multiple remote branches. Of course, the painful way is to create so many clones and then manage them individually in terms of directories.

But with git there is a better way -- managing with remotes. See for an introduction on working with remotes.

To achieve this, setup as many local tracking branches as the number of remote repositories that you want to checkin into:

$ git branch <lb1> <rb1>

Here lb1 and rb1 are local and remote branches. In practice, lb1 can be any name that can be easily memorised and rb1 can be "origin/". Of course origin is a reference depending on the protocol chosen for sharing the remote.

Now, you can just cherry-pick a change from one branch and commit it on a different branch - like so...

$ git branch
$ git checkout lb1 # changes are checked in already on lb1
$ git log --oneline

Now pick up the SHA corresponding to your checkin and continue ...

$ git fetch # just to make sure that you are on HEAD
$ git checkout lb2
$ git cherry-pick --no-commit SHA # SHA is the value copied from lb1

Now check and confirm that all your changes are in order. Finally commit and push ...

$ git commit
$ git push origin HEAD:refs/for/<branch name>

No comments:

Post a Comment