Jul. 21st, 2024
2024年 6月 16日

Post: Use different accounts for git on the same system

Use different accounts for git on the same system

Published 12:04 Apr 26, 2019.

Created by @ezra. Categorized in #Technology, and tagged as #UNIX/Linux.

Source format: Markdown

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/

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 (

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  
    User git  
    IdentityFile ~/.ssh/id_rsa

Now add the config for second Github account

# SomeAccountName account  
    User git  
    IdentityFile ~/.ssh/id_rsa

# example account  
Host github-example
    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 with the Hostname that you set up earlier in the config file and now it will look like this


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 =
    fetch = +refs/heads/*:refs/remotes/origin/*

for example, if the git url of you project is, and you added a ssh configuration like this:

# Bob's account  
Host github-bob
    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[](>" -m "anything"

See this for more about “author”:

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 "_email@example.com_"

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

$ git config

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 😎.

Pinned Message
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF