Setting up multiple user profiles in Git

This post should hopefully help you setup multiple user profiles when using Git on Windows.

I had already set up my Git installation to connect to GitHub using my personal email address. I then wanted to add another profile to connect to BitBucket using my work email address.

So, how did I do it?

Create a new pair of SSH Keys

The first step was to create a new pair of SSH keys using my work email address so I can connect to BitBucket. I am going to backup my current SSH keys just incase anything goes wrong. So open up your Git Bash console and type:

cd ~/.ssh
mkdir key_backup
cp id_rsa* key_backup

 

This will copy any SSH keys in the the "key_backup" directory we just created.

Now we need to create a new SSH key pair for my work email 

ssh-keygen -t rsa -C "MyWorkEmailAddress"

It will prompt you to give the key file a name:

Enter file which to save the key (/c/documents and settings/USERNAME/.ssh/id_rsa):

You need to specify a new name or it will overwrite your existing keys - which you don't want.

Enter a name of your choice - I used

id_rsa_work

You will then be prompted to enter a passphrase and your new key will be created.

 

Adding the Key to your BitBucket account 

You will need add the newly created SSH Key to your BitBucket account.

Naviagte to your /.ssh/ directory and open up the newly created key file in a text editor. It will be called what ever you name you gave it in the last step, so in my case it is called id_rsa_work.pub

Copy the entire line as it is, to your clipboard.

Log in to your BitBucket account and navigate to your "Account > SSH Keys" section.

Paste your key in to the text box and click Add Key


Adding the SSH keys to the SSH Agent

Now we need to add to the SSH Agent so it knows about the new key pair we have just created.

Still in your Git Bash console type:

ssh-agent.exe bash

Then type

ssh-add.exe ~/.ssh/id_rsa_work

I called my new key pair id_rsa_work but if you have given it a different name then use that one here.

Then type

exit

 

Configuring your config

In Windows Explorer navigate to your .ssh directory (usually in your profile directory)

Edit the file named "config" in a text editor. (If there isn't one there just create one)

You then need to add the following details to the file:

# Default GitHub user
Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa

# Work user account
Host bitbucket.org
  HostName bitbucket.org
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_work

 

This will tell Git that when I connect to the host github to use the id_rsa key pair but when I connect to bitbucket to use my id_rsa_work key pair.

Save and close the config file.

 

That is all that you should need to do. Now if I try to clone a repo from Github like this:

git clone https://github.com/MY_USER_NAME/MY_REPO_NAME.git

It will use my GitHub credentials and the SSH Keys created using my personal email.

However, cloning a repo from BitBucket like this:

git clone git@bitbucket.org:MY_USER_NAME/MY_REPO_NAME.git

It will use my BitBucket credentials and the SSH keys created using my work email.

 

Note:

This post is a bit rushed as I wanted to get out as soon as possible whilst still fresh in my mind!

Posted by codingbadger at 10:15
Tags :

0 Comments:

Post a comment

blog comments powered by Disqus

Authors

Recent Comments

Powered by Disqus