Skip to content

Instantly share code, notes, and snippets.

@rahularity
Last active November 15, 2024 20:41
Show Gist options
  • Save rahularity/86da20fe3858e6b311de068201d279e3 to your computer and use it in GitHub Desktop.
Save rahularity/86da20fe3858e6b311de068201d279e3 to your computer and use it in GitHub Desktop.
How To Work With Multiple Github Accounts on your PC

How To Work With Multiple Github Accounts on a single Machine

Let suppose I have two github accounts, https://github.com/rahul-office and https://github.com/rahul-personal. Now i want to setup my mac to easily talk to both the github accounts.

NOTE: This logic can be extended to more than two accounts also. :)

The setup can be done in 5 easy steps:

Steps:

  • Step 1 : Create SSH keys for all accounts
  • Step 2 : Add SSH keys to SSH Agent
  • Step 3 : Add SSH public key to the Github
  • Step 4 : Create a Config File and Make Host Entries
  • Step 5 : Cloning GitHub repositories using different accounts

Step 1

Create SSH keys for all accounts

First make sure your current directory is your .ssh folder.

     $ cd ~/.ssh

Syntax for generating unique ssh key for ann account is:

     ssh-keygen -t rsa -C "your-email-address" -f "github-username"

here,

-C stands for comment to help identify your ssh key

-f stands for the file name where your ssh key get saved

Now generating SSH keys for my two accounts

     ssh-keygen -t rsa -C "[email protected]" -f "github-rahul-office"
     ssh-keygen -t rsa -C "[email protected]" -f "github-rahul-personal"

Notice here rahul-office and rahul-work are the username of my github accounts corresponding to my_office_email@gmail.com and my_personal_email@gmail.com email ids respectively.

After entering the command the terminal will ask for passphrase, leave it empty and proceed.

Passphrase Image

Now after adding keys , in your .ssh folder, a public key and a private will get generated.

The public key will have an extention .pub and private key will be there without any extention both having same name which you have passed after -f option in the above command. (in my case github-rahul-office and github-rahu-personal)

Added Key Image


Step 2

Add SSH keys to SSH Agent

Now we have the keys but it cannot be used until we add them to the SSH Agent.

     ssh-add -K ~/.ssh/github-rahul-office
     ssh-add -K ~/.ssh/github-rahul-personal

You can read more about adding keys to SSH Agent here.


Step 3

Add SSH public key to the Github

For the next step we need to add our public key (that we have generated in our previous step) and add it to corresponding github accounts.

For doing this we need to:

1. Copy the public key

 We can copy the public key either by opening the github-rahul-office.pub file in vim and then copying the content of it.
     vim ~/.ssh/github-rahul-office.pub
     vim ~/.ssh/github-rahul-personal.pub

OR

We can directly copy the content of the public key file in the clipboard.

     pbcopy < ~/.ssh/github-rahul-office.pub
     pbcopy < ~/.ssh/github-rahul-personal.pub

2. Paste the public key on Github

  • Sign in to Github Account
  • Goto Settings > SSH and GPG keys > New SSH Key
  • Paste your copied public key and give it a Title of your choice.

OR


Step 4

Create a Config File and Make Host Entries

The ~/.ssh/config file allows us specify many config options for SSH.

If config file not already exists then create one (make sure you are in ~/.ssh directory)

     touch config

The commands below opens config in your default editor....Likely TextEdit, VS Code.

     open config

Now we need to add these lines to the file, each block corresponding to each account we created earlier.

     #rahul-office account
     Host github.com-rahul-office
          HostName github.com
          User git
          IdentityFile ~/.ssh/github-rahul-office

     #rahul-personal account
     Host github.com-rahul-personal
          HostName github.com
          User git
          IdentityFile ~/.ssh/github-rahul-personal

Step 5

Cloning GitHub repositories using different accounts

So we are done with our setups and now its time to see it in action. We will clone a repository using one of the account we have added.

Make a new project folder where you want to clone your repository and go to that directory from your terminal.

For Example: I am making a repository on my personal github account and naming it TestRepo Now for cloning the repo use the below command:

    git clone [email protected]{your-username}:{owner-user-name}/{the-repo-name}.git

    [e.g.] git clone [email protected]:rahul-personal/TestRepo.git

Finally

From now on, to ensure that our commits and pushes from each repository on the system uses the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.

To do this use the following commands.

     git config user.email "[email protected]"
     git config user.name "Rahul Pandey"
     
     git config user.email "[email protected]"
     git config user.name "Rahul Pandey"

Pick the correct pair for your repository accordingly.

To push or pull to the correct account we need to add the remote origin to the project

     git remote add origin [email protected]:rahul-personal
     
     git remote add origin [email protected]:rahul-office

Now you can use:

     git push
     
     git pull

P.S:
If this gist has been helpful to you, kindly consider leaving a star.
If you'd like, let's connect on LinkedIn and build a supportive community together.

@rcodina-jelou
Copy link

A better way IMO

I created one ssh key for each account like in this gist. Then used this to have different .gitconfig files (global, work, personal): https://dev.to/equiman/how-to-use-multiple-users-with-git-2e9l Tks @anteromano

And in each "local" (work/personal) .gitconfig I added:

[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_personal -F /dev/null
[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_work -F /dev/null

Now I just need to git pull and push and will go to where I want depending the parent directory I am. I think it's better.

ok, but what is dev/null?

@fguillen-getsafe
Copy link

It didn't work to me until I added the IdentitiesOnly:

# ~/.ssh/config

#myuser account
Host github.com
     HostName github.com
     User git
     IdentityFile ~/.ssh/id_rsa
     IdentitiesOnly yes

#myuser-work account
Host github.com-myuser-work
     HostName github.com
     User git
     IdentityFile ~/.ssh/id_rsa_work
     IdentitiesOnly yes

@fguillen-getsafe
Copy link

This command helped me to debug my tests:

GIT_SSH_COMMAND="ssh -v" git pull

@iamjayshree
Copy link

this is one of best posts on adding ssh keys. Thankyou!

@Geoffroy94
Copy link

Have followed this strictly and still getting error : remote: Write access to repository not granted.

@alphaGithub
Copy link

@jyan-micro1
Copy link

Thanks!

@Allan-Morales-Prado
Copy link

ssh-add -K ~/.ssh/github-Rahul-office
ssh-add -K ~/.ssh/github-Rahul-personal

-K option for what, that's necessary?
I've installed OpenSSH for Windows and run it on Windows PowerShell:

ssh-add -K ...\private-key-file-name

But this command returns:
unknown option -- K

@lakshmadu
Copy link

when you run the following command(step 2) if you get error like this, you can use this solution:-

$ ssh-add -K ~/.ssh/{yourusername}

Error:- Could not open a connection to your authentication agent.

Solution :- The error message you're seeing, "Could not open a connection to your authentication agent," typically occurs when the SSH agent isn't running or the connection to the agent isn't correctly established. To resolve this issue, you can try the following steps:

  1. Start the SSH agent by running eval $(ssh-agent -s) in your terminal.
  2. Then, add your SSH key again using ssh-add ~/.ssh/lakshmadu.

This should allow you to add the key without encountering the error.

@youfoundmalik
Copy link

FOR EXISTING PROJECTS: To push or pull to the correct account we need to reset the remote origin of the project
git remote set-url origin git@github-account-profile:username/repo-name.git

@RoydBOL
Copy link

RoydBOL commented Sep 4, 2024

Thankyou so much for the blog, save me so much of time.

@imgustavo
Copy link

Thank you so much for this. Again and again

@imgustavo
Copy link

[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_work -F /dev/nul

thats only for mac

@ahmadmohammadirad2006
Copy link

Really clear explanation
Thanks

@fguillen
Copy link

This is gold

@EJEASIntercom
Copy link

I've always wanted to use multiple Github Accounts on my computer, and now I finally found a way, thank you. EJEAS

@soumyadeepsp
Copy link

You're a f*cking life-saver legend brooooooo

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