Push to a remote branch that already exists locally

2 min read | by Jordi Prats

In cas we need to have multiple remote repositories it's branches names are likely to clash. A clear example it the master branch. How can we switch between branches from remote repositories if they have the same name?

To do so we can add the remote repository as usual:

git remote add pet2cattle git@github.com:jordiprats/flask-pet2cattle.git
git fetch general

To be able to checkout the master branch for the new remote repository, if it clashes with an existing one, we will need to specify a new branch name using the -b option. For example, to create a branch called pet2cattle_master using the master branch from the remote pet2cattle* we would do it like so:

git checkout -b pet2cattle_master pet2cattle/master

Once we are done with the changes, to be able to push them to the correct remote we will have to specify the target branch name using the HEAD:master syntax:

git push general HEAD:master

Posted on 08/07/2021

Categories