Config multiple Git accounts

City South
3 min readJan 17, 2024

As a programmer, we may have several git accounts, some are used in our daily work, and some are for personal study. These Git accounts are on different platforms, such as GitHub, Gitee, Bitbucket, etc. But there is only one computer, how we can quickly and effectively to submit code in different projects with different git accounts? We can configure the SSH keys.

Clear the git global configuration

If we’ve set up the global user.name and user.email with below git commands before, we need to delete the configuration first.

git config --global user.name "your_name"
git config --global user.email "your_email"

If you don’t know if you’ve set it up before, you can use below command to check it and use the following commands to delete them.

# check the global configuration
git config --global --list
# delete the global configuration for user.name
git config --global --unset user.name
# delete the global configuration for user.email
git config --global --unset user.email

In fact, these configuration data are stored in this path C:\Users\<user_name>\.gitconfig(Windows OS)by default, if you didn’t specify it before. You can find the file via the path and open it with Notepad and modify it.

Configure SSH Keys

Generate SSH Keys

First you need to generate SSH Keys. Go to the folder C:\Users\<user_name>\.ssh. If there is no folder .ssh there, create one. Then, use this command ssh-keygen -t rsa -C "new email"in CMD to generate the public and private key pair. The file with extension.pub is public key.

Add the private key file to the SSH Agent

By default, the ssh-agent will only read id_rsa file. In order for it to recognize our newly generated private keys, we need to use ssh-addto add these private keys to the ssh-agent.

If you get a "Could not open a connection to your authentication agent" error when adding, run the command ssh-agent bash first, and then add it.

If you encounter the error "unable to start ssh-agent service, error :1058"when running command ssh-agent bash is unable to start ssh-agent service, error :1058,please check the service "OpenSSH Authentication Agent"and make sure it’s running. ref:https://stackoverflow.com/questions/52113738/starting-ssh-agent-on-windows-10-fails-unable-to-start-ssh-agent-service-erro

Configure the config file

Afterwards, we need to update the config file. Please check if the config file exists in the .ssh folder. If no, create one. Then follow the below content to update the config file with your data.

# Default github 
Host github.com
HostName github.com
User "<user_name>"
IdentityFile ~/.ssh/id_rsa_github

# gitee
Host gitee.com
HostName gitee.com
User "<user_name>"
IdentityFile ~/.ssh/id_rsa_gitee

Add the public key to the Git system

Normally, all Git systems should have a place to add SSH Key, e.g. GitHub https://github.com/settings/keys. When adding a new SSH Key, copy and paste the content from .pub file which was generated previously.

Test the configuration

Finally, we can use the command ssh -T to test if the configuration is OK.

In the future, if you have a new Git account, you can follow the same steps to add and set up a new account. Never configure global user.name and user.email for git. We just need to set up the local user.name and user.email in different repositories.

git config user.name "your_name"
git config user.email "your_email"

Thanks!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

City South
City South

No responses yet

Write a response