Pushing to Multiple Git Remotes
Updated: 14th of September 2020
Published: 31st of August 2020
I am working on a project where I need to push to both a github and bitbucket repository as the project is stored in both of these locations. I do not have the ability to sync between the two using either github or bitbuckets built in tools. This post goes through the process of setting up your git environment in order to easily push to both repository providers.
Software Used in this Post
The following software versions where used in this post.
- Ubuntu - 2004
- git - 2.25.1
When authenticating to Github and Bitbucket I have to use two different authentication methods. For Github I am using SSH key based authentication and for Bitbucket I am using HTTP based authentication.
I wont re-hash the method to use SSH keys for authentication on github. The process is covered well in this github article.
To make authenticating to Bitbucket more user friendly I will be using the git credential store. This allows you to store you credentials in your home directory so that you dont have to enter your username/password each time you access the repository over HTTP.
git config command to enable the
git config --global credential.helper store
Enable the required remotes in the local config. I am first cloning down the repo from Github. This sets up the Github repo as the origin remote.
# github clone email@example.com firstname.lastname@example.org:<user-name>/<repo-name>.git
Next up add the bitbucket remote.
# bitbucket git remote add bitbucket https://bitbucket.org/scm/~<user-name>/<repo-name>.git
Confirm that your remotes are correct.
git remote -v # output origin email@example.com:<user-name>/<repo-name>.git (fetch) origin firstname.lastname@example.org:<user-name>/<repo-name>.git (push) bitbucket https://bitbucket.org/scm/~<user-name>/<repo-name>.git (fetch) bitbucket https://bitbucket.org/scm/~<user-name>/<repo-name>.git (push)
Enable the the credentials store for the project.
git config credential.helper store
The first time you connect a remote git will ask you for your credentials. The credentials will be stored in the ~/.git-credentials file and the permissions will be set to 0600 so only you have rw access.
Now for the magicgit alias incantation. Create a git alias named pushall that will cycle through the remotes pushing changes to all of them.
git config --global alias.pushall '!git remote | xargs -L1 git push --all'
When you want to push to the remotes you use the
command and voila! Git will connect to each one pushing the changes to
In this post I showed you how to push a local git repository to multiple git service providers using different authentication methods.