Use different accounts for git on the same system

4 mins read Apr 26, 2019

Use different accounts for git on the same system

Table of Content

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.

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 the Github guide to add SSH key on your 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:

Identity added: /path/to/your/id_rsa_example ([email protected])

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

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

Now add the config for second Github account

# SomeAccountName account  
Host github.com-YourAccountName1  
    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.

If your project did not use git yet

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:username/Test.git

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

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

If you already cloned a project

just edit {YOUR_PROJECT_LOCATION}/.git/config, change (or add) [remote "origin"] part to something like this:

[remote "origin"]
	url = [email protected]:username/Test.git
	fetch = +refs/heads/*:refs/remotes/origin/*

for example, if the git url of you project is https://github.com/BigBob/Template.git, and you added a ssh configuration like this:

# Bob's account  
Host github-bob
    HostName github.com  
    User git  
    IdentityFile ~/.ssh/id_rsa_bob

then you need to change the url part to url = git@github-bob:BigBob/Template.git.

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](mailto:[email protected])>" -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 protected]_"

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

$ git config user.email [email protected]

Now remaining things are the same as you do

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

Ohhh, We did it.

Thanks for Reading, Hope you enjoyed 😎.

Picsew | Ezra
« Newer: I Wish  Older: Git Installation for Linux and Unix »