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 .

cmd
git config --global credential.helper store

Git 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.

cmd
# github
  
clone git@github.com git@github.com:<user-name>/<repo-name>.git

Next up add the bitbucket remote.

cmd
# bitbucket
 
git remote add bitbucket https://bitbucket.org/scm/~<user-name>/<repo-name>.git

Confirm that your remotes are correct.

cmd
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.

cmd
git config credential.helper store
Note
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.

Git 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.

cmd
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.

cmd
git pushall

Outro

In this post I showed you how to push a local git repository to multiple git service providers using different authentication methods.