Image source: https://dev.to/ech0server/multiple-github-accounts-in-the-same-computer

Git Push & Pull with Two different accounts and Two different users on the same machine

Rajan Maurya
6 min readOct 29, 2017

--

I don’t know how many of you, have ever tried to Push or Pull your code from two different Github account on same system or tried to Push code with as two different user.

As everyone knows we celebrate Hacktoberfest whole October. I don’t know how many of you participated in it but I am doing from last year and every time my friends got email as they complete 4 PRs in last week of month and instead of that I complete my 4 PRs in the first week but did not get email in both times and I have to email them to remind. Hey, I am alive and want cool goodies for fun.

This was really fun to learn, I came home today and made my mind. I will send 4 PRs with different accounts too and will get extra t-shirts, I wasn’t aware I will learn this cool thing because of my childish 😬 thing.

https://blog.medisin.ntnu.no/does-daycare-cause-stress-in-small-children/?lang=en

Now before sending 4 PRs with different Github accounts. I got another main task.

  1. Pushing Code on two different accounts on the same system.
  2. Pushing Code with a different user.

I think this is a good thing as a developer to know this. Let’s start with the first task.

# Pushing Code on two different accounts from the same system.

As we know we can push code on Github repository with HTTP or SSH. I prefer SSH because we need to configure an account only once. Yes, If you are right we want to push on different account means we need to generate SSH key for every account.

Step 1: Now generate an SSH key for second accounts

$ ssh-keygen -t rsa -C "Github-email-address"

It will ask you file to save SSH key, Don’t forget to change

{Home Directory}/.ssh/id_rsa -> {Home Directory}/.ssh/id_rsa_example (Any file name)

Now enter the passphrase and It’s done and you can get the key at

{Home Directory}/.ssh/id_rsa_example (File name you entered)

Step 2: Now It time to attach the key with your second Github account.

Copy your SSH key using below command

$ pbcopy < ~/.ssh/id_rsa_example.pub

and follow this Github guide to add SSH key on your account -> https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

Now you have set up the SSH key, Now It’s time tell SSH about it

Next, because we saved our key with a unique name, we need to tell SSH about it. Within the Terminal, type: ssh-add ~/.ssh/id_rsa_example. If successful, you'll see a response of

Step 3: Setup Github Host

Everything is done, Now we need to configure when we want to push code on our first account and when we want to push on our second account. let’s create a config file and open

$ touch ~/.ssh/config

You can open from any editor, I used the sublime text editor

$ subl config

you will get default config settings there of you first Github account

#therajanmaurya account
Host github.com-therajanmaurya
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Now add the config for second Github account

#therajanmaurya account
Host github.com-therajanmaurya
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
#example account
Host github-example
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_example

write any Hostname carefully and remember, it will be your identifier that will tell you on which you want to push and don’t forget to write correct IdentityFile name that you created earlier.

Now Save and close 😎.

Step 4: Push code on a second account with any Github user.

Now we are done with SSH setup on the local machine. It’s time to push the code on Github.

Go to the working directory of your project that you want to push on second Github account and initialize the Git

$ git init

It’s time to add remote of the second Github account project

$ git remote add origin git@github-example:example/Test.git

Be careful with the Host, this is the default host.

git@github.com 

you have to replace “github.com” with the Hostname that you set up earlier in the config file and now it will look like this

git@github-example

Step 5: Push code with a different user.

Now you can push your code with three different manner

  1. Push code with already present Github user.
  2. Push code with the second user but with first Github user
  3. Push code with second Github user that you set up now.

Now Let’s start one by one and push code on Github

# Push code with already present Github user.

Lol, you don’t need to do anything for this, simply add and commit your changes like this

$ git add .
$ git commit -m "commit message"

# Push code with the second user but with first Github user

In this case, you need to mention the author from which you want to commit the changes

$ git add
$ git commit --author="Example <example@gmail.com>" -m "anything"

See this for more about “author”: https://stackoverflow.com/a/11579722/3176277

# Push code with second Github user that you set up earlier.

If you want to commit with the second user and don’t want to link with any user then you have to set up user email to the specific project

$ git config user.email "email@example.com"

and Confirm that you have set the email address correctly in Git:

$ git config user.email
email@example.com

Now remaining things are the same as you do

$ git add .
$ git commit -m "anything"

Update

You may remember that we have setup the ssh key on github.com account but we did nothing to setup ssh locally so Github.com can’t verify account while pushing your commits.

As we did not added known host info so you will be getting this

To fix this, open your project root git directory and enter below command

$ ssh-keygen -R github.com

You will get this

It means you have added ssh key as know host.

Ohhh, We did it

https://tenor.com/view/finally-yas-yes-hallelujah-celebration-gif-4562903

Thanks for Reading, Hope you enjoyed 😎.

--

--

Rajan Maurya

Senior Software Engineer at OpenLane Inc, Open source contributor at Mifos Initiative and mentoring GSoC Students. https://github.com/therajanmaurya