Skip to content

Instantly share code, notes, and snippets.

@rosswd
Last active September 8, 2024 01:51
Show Gist options
  • Save rosswd/e1afd2b0b0d515517eac to your computer and use it in GitHub Desktop.
Save rosswd/e1afd2b0b0d515517eac to your computer and use it in GitHub Desktop.
Setting up a Github and Bitbucket account on the same computer on Mac OS. Now with a guide for Windows 10.

Setting up a seperate Github and Bitbucket account

If you want to have private repositories at Bitbucket and public repositories at Github, then this is the guide for you.

This guide assumes:

  • The computer is running Mac OSX 10.7 or greater; The computer is running Windows 10
  • You already have an account at Github and another at Bitbucket

Setting up github and bitbucket on the same computer (Windows)

Guide for Windows

mix3d asked for some help using this guide with windows so here we go. This was tested with Windows 10. Run all commands in Git Bash once it's installed.

Github will be the main account and bitbucket the secondary.

Git for Windows

  • Download and install Git for Windows
    • In the installer, select everything but decide if you want a desktop icon (2nd step)
    • Select your editor, I use Visual Studio Code
    • Choose Git from the command line... on the PATH environment step
    • Choose Use the OpenSSL library
    • Choose Checkout Windows style, commit Unix-style line endings
    • Choose Use minTTY (the default terminal editor of MSYS2)
    • Deselect Enable symbolic links if checked
    • Don't enable experimental options
    • Click Install and then Finish

Configure Git

  • git config --global user.name "Your Name"
  • git config --global user.email "username@email.com"

Confirm changes: git config --global -l

Create SSH Keys

  • Right click on desktop and choose Git Bash here

  • Enter cd to get to home directory (c:/Users/[username])

    ssh-keygen -t rsa -b 4096 -C "your github email"

Enter passphrase when prompted

Save keys to: ~/.ssh/id_rsa

Repeat for bitbucket: ssh-keygen -t rsa -b 4096 -C "bitbucket email"

Save bitbucket key to ~/.ssh/id_rsa_bb

Auto run ssh-agent

  • Make the ssh agent auto run:
    • Check for a .profile or .bashrc file in your home directory (c:/Users/[username])
    • Open the file in your editor if found or create a new file in your editor if not
    • Place the following code in your file: Auto-launching ssh-agent...
    • Save the file (I used .profile)
    • Quit Git Bash

Attach Keys

Login to remote git provider and add ssh key:

clip < ~/.ssh/id_rsa.pub
clip < ~/.ssh/id_rsa_bb.pub

Obviously run each clip command individually.

Paste into text area, under ssh settings, in your github or bitbucket account. Also give the ssh key a title like "your name" Laptop.

Create Config file

I am using vim, enter your editor here if different:

vim ~/.ssh/config

Create your git aliases like so:

# Github (default)
  Host gh
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

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

Add the identities to SSH:

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_bb

Enter passphrase if prompted.

Check keys were added:

ssh-add -l

Check that remote git provider recognizes keys:

ssh -T gh
ssh -T bb

Setting up github and bitbucket on the same computer (Mac OS)

Github will be the main account and bitbucket the secondary.

Intall Git

Use Homebrew to install Git.

Configure Git

  • git config --global user.name "Your Name"
  • git config --global user.email "username@email.com"

Confirm changes: git config --global -l

Create SSH Keys

ssh-keygen -t rsa -C "github email"

Enter passphrase when prompted. If you see an option to save the passphrase in your keychain, do it for an easier life.

Save keys to:

~/.ssh/id_rsa

Repeat for bitbucket:

ssh-keygen -t rsa -C "bitbucket email"

Save bitbucket key to ~/.ssh/id_rsa_bb

Attach Keys

Login to remote repo and add ssh key:

pbcopy < ~/.ssh/id_rsa.pub
pbcopy < ~/.ssh/id_rsa_bb.pub

Obviously run each pbcopy command individually.

Paste into text area, under ssh settings, in your github or bitbucket account. Also give the ssh key a title like "your name" Laptop.

Create Config file

I am using vim, enter your editor here if different:

vim ~/.ssh/config

Create your git aliases like so:

#Github (default)
  Host gh
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

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

Note: On Mac OS Sierra onwards you have to add this to the top of the config file:

Host *
  UseKeychain yes
  AddKeysToAgent yes

Add the identities to SSH:

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_bb

Enter passphrase if prompted.

Check keys were added:

ssh-add -l

Check that repo recognizes keys:

ssh -T gh
ssh -T bb

Using new dual setup

This guide assumes you have followed multi-git.md if on a Unix system or multi-git-win.md for a Windows system and set up both accounts.

Github (default)

Create a repo online called testmulti (or one of your choosing), then in Terminal, create your repository:

mkdir ~/testmulti
cd ~/testmulti
touch readme.md
git init
git add .
git commit -am "first commit"
git remote add origin git@gh:username/testmulti.git
git push origin master

Add some text to readme on github.com, then:

git fetch origin master
git diff master origin/master
git merge origin/master
git push origin master

Bitbucket (secondary)

Create a repo online called testbucket and then in Terminal:

mkdir ~/testbucket
cd ~/testbucket
touch readme.md
git init

This being the secondary account, the username and email have to be overwritten, using secondary account values, at the repo level:

git config user.name "Full Name"
git config user.email email_address

This must be done once for every bitbucket (or secondary) repo, it is not needed for github (or primary) repos because the global is used in that scenario. There may be a cleaner way to do this but right now it works okay.

Just to be clear you do not need to change these values back afterwards because the global values (which apply to all future repos created) will be set.

git add .
git commit -am "first commit"
git remote add origin git@bb:username/testbucket.git
git push origin master

Add some text to readme on bitbucket.org, then:

git fetch origin master
git diff master origin/master
git merge origin/master
git push origin master

Use case: Repo already exists on secondary remote repo (bitbucket)

So you have a repository that already exists and you want to want to clone it but also you want to make sure when you push it, the correct user, the bitbucket user pushes. Let's say the repo is called booker.

git clone git@bb:bb_username/booker.git
git config user.name "bitbucket user name"
git config user.email email_address

Remember, because we are using the secondary account, we have to over-ride the global configuration of username and email. The git clone command is using the ssh keyfile you set up earlier and is doing the transfer over ssh.

Now, change into the cloned directory and modify one of the files. Use git status to check the current state, then use git add 'filename' and git commit -m "commit message".

Finally push your changes using git push origin master.

@PatriciaFontanillo
Copy link

Thank you very much. Very useful

@SIMONNABUKO
Copy link

Hi, username You've successfully authenticated, but GitHub does not provide shell access.

I get the above when I try to check if the keys are recognized(ssh -T gh
ssh -T bb)

What am I doing wrong

@testDrive64
Copy link

Thanks really helpful.

@ehelaya
Copy link

ehelaya commented Jan 20, 2022

Excellent guide. Thanks.

@mveer-jaipur
Copy link

Thanks for excellent guide.

Could you please add eval ssh-agent -s to start agen.

@designlobby
Copy link

Great guide, thank you.

FYI as per stackoverflow on github instructions 'master' should be 'main'. I was getting git error: failed to push some refs to remote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment