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.
The following software versions where used in this post.
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.
Use the git config command to enable the credential store .
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 git@github.com git@github.com:<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 git@github.com:<user-name>/<repo-name>.git (fetch)
origin git@github.com:<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
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 pushall
In this post I showed you how to push a local git repository to multiple git service providers using different authentication methods.
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