Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

bitcoin:bc1q95kxs25a6d7fp8prqdmgm6sstjxav8q959t30u

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

bitcoin:bc1q95kxs25a6d7fp8prqdmgm6sstjxav8q959t30u

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

bitcoin:bc1q95kxs25a6d7fp8prqdmgm6sstjxav8q959t30u

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.

bitcoin:bc1q95kxs25a6d7fp8prqdmgm6sstjxav8q959t30u

@xapou

This comment has been minimized.

Copy link

commented Feb 27, 2016

Very clear and informative. Thank You.

Quick additional tip
In ~/.ssh/config, if you name your host like the hostname...

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

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

...you can directly use the links provided by Github or Bitbucket !

git clone git@bitbucket.org:bb_username/booker.git
@rosswd

This comment has been minimized.

Copy link
Owner Author

commented Mar 16, 2016

I am glad you found it helpful. Good thinking on using links provided by providers, I hadn't considered that because I usually just looked at the URL and typed it in manually.

@nasr18

This comment has been minimized.

Copy link

commented Mar 18, 2016

i forgot my private key for bitbucket account. how do i recover that????? please reply fast.

@nasr18

This comment has been minimized.

Copy link

commented Mar 18, 2016

now i can't able to push or fetch from bitbucket account. please anyone reply. how to overcome this problem. I can't remember my private key.

@robertpetras

This comment has been minimized.

Copy link

commented May 8, 2016

Thank you for this comprehensive instruction on how to set up multiple ssh keys and run them on the same computer for Git and Bitbucket. I found it very useful. Works like a charm! 👍 I was looking for such like this Gist because the text editor of my choice, PHPStorm only wanted to connect with the default RSA key until I created the config file for the SSH.

@brandosaurus-rex

This comment has been minimized.

Copy link

commented Jun 6, 2016

Is it possible to do this method with heroku as well?? so i would have github, bitbucket, and heroku working this way.

I tried it and im not sure if it works or not. The message I get is....

Brandons-MacBook-Pro:~ Brandon$ ssh -T h
Warning: Permanently added the RSA host key for IP address '50.19.85.156' to the list of known hosts.
shell request failed on channel 0

@rosswd

This comment has been minimized.

Copy link
Owner Author

commented Jun 11, 2016

Man, I wish Github would mail me or something when new comments are added to gists. sigh.

@robertpetras no problem, glad it helped.

@progus-florian The first thing to do when trying to diagnose ssh connection problems is to run the ssh client in verbose mode. Try using the -v (verbose option) with ssh to see what's happening when you connect. From the man page:

-v' Verbose mode. Causes ssh to print debugging messages about its progress. This is helpful in debugging connection, authentication, and configuration problems. Multiple -v options increase the verbosity. The maximum is 3.

It should be possible to run Heroku this way but it's been a while since I used Heroku. I seem to remember there being some kind of special ssh thing it does with git, to deploy your changes. If you can't fix it, post back and I will have a look.

Some quick things you could try:

  1. Check that you copied the public key from your computer to heroku
  2. Connect using verbose mode as above ssh -v <heroku-user>@<heroku-ip-address> /path/to/local/heroku/key_file
  3. Check that the username and Identity file is correct in ~/.ssh/config
@comtizen

This comment has been minimized.

Copy link

commented Aug 10, 2016

Thank you.
I found it very useful. 👍

@mhetreramesh

This comment has been minimized.

Copy link

commented Oct 16, 2016

This is useful, thank you!

@rupeshpatil

This comment has been minimized.

Copy link

commented Nov 30, 2016

Thanks, Useful information.

@dysol

This comment has been minimized.

Copy link

commented Feb 14, 2017

Latest macOS 10.12 seems to cause issue with this. Whenever my computer restarts, it prompts for SSH Key passphrase.

@rosswd

This comment has been minimized.

Copy link
Owner Author

commented Feb 17, 2017

@dysol I’m on 10.10 (Yosemite) so I can’t test. Based on some limited searches there seems to be an issue with macos sierra not saving keys into the keychain. There is a possible workaround with the usual if something breaks, hey I warned ya disclaimer. Inside your ~/.ssh/config file add the following to the beginning of the file:

Host *
  UseKeychain yes
  AddKeysToAgent yes

You may need to add a path to each of your identities as well, like IdentityFile /path/to/id_rsa

Ref:

@MostafaEzzelden

This comment has been minimized.

Copy link

commented Jan 13, 2018

Thank you. I found it very useful

@serkansokmen

This comment has been minimized.

Copy link

commented Jan 31, 2018

Saved my life! Thank you :)

@vsaggu

This comment has been minimized.

Copy link

commented Mar 2, 2018

Good job.

@nachomacaso

This comment has been minimized.

Copy link

commented Apr 16, 2018

When trying to push to bitbucket, I'm getting this error:
$ git push origin dev
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.

Anyone know a fix or have encountered this?

@cj-tww

This comment has been minimized.

Copy link

commented Jun 29, 2018

@nachomacaso - Replace ~/.ssh/id_rsa with the full path to your key.
"Optionally replace ~/.ssh/id_rsa with the path to your key. To add additional keys, add a new line for each key with: IdentityFile /path/to/your_key."
-- from @rosswd's comment.
I can confirm it worked.

@LucasCaixeta

This comment has been minimized.

Copy link

commented Jul 10, 2018

Your post is really useful. May I put this content in an article at medium? sure, with all references for this repository.

@rosswd

This comment has been minimized.

Copy link
Owner Author

commented Aug 1, 2018

@LucasCaixeta Yes that is fine, thanks for asking.

@mix3d

This comment has been minimized.

Copy link

commented Aug 10, 2018

Any suggestions for the same, but on Windows?

@rosswd

This comment has been minimized.

Copy link
Owner Author

commented Aug 13, 2018

@mix3d I updated the guide with a Windows section multi-git-win.md. Enjoy!

@nickangtc

This comment has been minimized.

Copy link

commented Dec 2, 2018

Wonderful, thanks @rosswd for this clear writeup. Got up and running with Bitbucket for the first time (for a free private repo) in 10 minutes using this 👍

@d-beloved

This comment has been minimized.

Copy link

commented Mar 8, 2019

Awesome writeup, a big thanks to you. 💯

@CarlosSR

This comment has been minimized.

Copy link

commented May 1, 2019

Thank you!!!! works awesome!

@charlesdebarros

This comment has been minimized.

Copy link

commented Aug 16, 2019

This is a life saver! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.