Pushing to Multiple Git Remotes
Updated: 2020-09-14
Published: 2020-08-31
Intro
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
Credentials
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.
Github Authentication
I wont re-hash the method to use SSH keys for authentication on github. The process is covered well in this github article.
Bitbucket Authentication
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.
Use the git config command to enable the credential store.
git config --global credential.helper storeGit Remotes
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 protected] [email protected]:<user-name>/<repo-name>.gitNext up add the bitbucket remote.
# bitbucket
git remote add bitbucket https://bitbucket.org/scm/~<user-name>/<repo-name>.gitConfirm that your remotes are correct.
git remote -v
# output
origin [email protected]:<user-name>/<repo-name>.git (fetch)
origin [email protected]:<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 storeGit Alias
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 git pushall command and voila! Git will connect to each one pushing the changes to them.
git pushallOutro
In this post I showed you how to push a local git repository to multiple git service providers using different authentication methods.
Links
https://www.shellhacks.com/git-config-username-password-store-credentials/
https://stackoverflow.com/questions/5785549/able-to-push-to-all-git-remotes-with-the-one-command
https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh