Scott Spence

Scott's Digital Garden.

Setting up multiple Git providers

Over the past couple of weeks now I have set up several development machines at work and have had to also use two git accounts, GitHib and Bitbucket.

To connect to both I use SSH as a preference, I have been using SSH in place of HTTPS for quite some time now, if you want to connect repeatedly without having to provide user name and password details then SSH is a good option.

If you are unfamiliar with using SSH to authenticate with git then take a look at my cheat sheets repository (ss10.me/cheat-sheets) there are several sections covering SSH, notably:

I have come across this set-up a few times now and implemented it for myself.

You'll need to create a config file in the .ssh folder in your home directory (Windows, Ubuntu or both if you use a WSL set-up) check with:

1ll ~/.ssh/

This will list out the contents of the folder, if you get No such file or directory then you don't have SSH configured.

Take a look at the How to Authenticate with GitHub Using SSH section on the cheat-sheets repo for details on that.

For this example let's presume that we have already created our SSH keys for Bitbucket and GitHub and authenticated with both Bitbucket and GitHub.

Next create a config file:

1nano ~/.ssh/config

This will open a new file with nano that we can add the following to:

1# Bitbucket (default)
2 Host bb
3 HostName bitbucket.org
4 User git
5 IdentityFile ~/.ssh/id_default
6
7# Github (secondary)
8 Host gh
9 HostName github.com
10 User git
11 IdentityFile ~/.ssh/id_secondary

Just remember that the IdentityFile needs to match what you have called your SSH keys.

Check current permissions with stat:

1# stat -c "%a %n" ~/.ssh/*
2644 /home/scott/.ssh/config
3700 /home/scott/.ssh/id_default
4700 /home/scott/.ssh/id_default.pub
5700 /home/scott/.ssh/id_secondary
6700 /home/scott/.ssh/id_secondary.pub

Change the config file permissions as needed:

1chmod 644 ~/.ssh/config

Multiple users

Ok now we should be able to push and pull to the respective GitHub and Bitbucket repositories, but unless you have the same username and email address for GitHub and Bitbucket then we're also going to need to specify specific user details for the repositories we're accessing, otherwise the details specified in the ~/.gitconfig are what will be used with commits.

In my case for work my default user account is Bitbucket so that is what I have specified in the ~/.gitconfig, however for the GitHub repos I work on I'll need to specify my GitHub credentials on a repo by repo basis.

Historically I have gone into the individual repo and manually set the config details.

1# from the root of the repo you want to specify the credentials
2git config user.name 'Your Name'
3git config user.email 'your@email.com'

I have since found an ok solution here: https://stackoverflow.com/a/43654115/1138354

Example:

Global config ~/.gitconfig

1[user]
2 name = play.user
3 email = play.user@gmail.com
4
5[includeIf "gitdir:~/work/"]
6 path = ~/work/.gitconfig

Work specific config ~/work/.gitconfig

1[user]
2 name = work.user
3 email = work.user@megacorp.ltd