Skip Navigation

Scott Spence

Setting up multiple Git providers

3 min read
Hey! Thanks for stopping by! Just a word of warning, this post is about 5 years old, wow! If there's technical information in here it's more than likely out of date.

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, GitHub 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:

ll ~/.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:

nano ~/.ssh/config

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

# Bitbucket (default)
  Host bb
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/id_default

# GitHub (secondary)
  Host gh
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_secondary

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

Check current permissions with stat:

# stat -c "%a %n" ~/.ssh/*
644 /home/scott/.ssh/config
700 /home/scott/.ssh/id_default
700 /home/scott/.ssh/id_default.pub
700 /home/scott/.ssh/id_secondary
700 /home/scott/.ssh/id_secondary.pub

Change the config file permissions as needed:

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

# from the root of the repo you want to specify the credentials
git config user.name 'Your Name'
git config user.email '[email protected]'

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

Example:

Global config ~/.gitconfig


[user]
  name = play.user
  email = [email protected]

[includeIf "gitdir:~/work/"]
  path = ~/work/.gitconfig

Work specific config ~/work/.gitconfig

[user]
  name = work.user
  email = [email protected]

There's a reactions leaderboard you can check out too.

Copyright © 2017 - 2024 - All rights reserved Scott Spence