Skip to content

Latest commit

 

History

History
155 lines (93 loc) · 4.35 KB

File metadata and controls

155 lines (93 loc) · 4.35 KB

Multiple SSH Keys settings for different github account

English Version

If you are really unfamiliar with this, I suggest watching the video 😄

Foreword

Sometimes we may encounter a situation where we need to have multiple GitHub accounts on the same computer. Why?

When your work computer and your own side project development are on the same computer, you will need the help of this article 😙

Trust me, you don't want to use your company's account to push commits to your own side project 😅

Tutorial

First, generate an ssh key. If you don't know how to generate one,

you can refer to Generating a new SSH key or Basic GitHub Tutorial - From Scratch.

Open your Git Bash.

ssh-keygen -t rsa -b 4096 -C "blue555236sa56@gmail.com"

(Please replace the email with your own)

The key point here is to remember to enter a new rsa name yourself, like /c/Users/twtrubiks/.ssh/id_rsa_rubik in the picture below.

This way, you won't overwrite the old rsa.

alt tag

Next, you will have a new file you just created in your .ssh path.

alt tag

Then paste your id_rsa_rubik.pub key to your GitHub.

alt tag

Usually, when you add it successfully, you will receive an email.

Then enter the command below:

ssh-agent -s

The above command must be executed. If you skip this step, the following error will occur:

Could not open a connection to your authentication agent.

So please remember to execute it.

Create a file named config in the .ssh directory (the file name is config, no extension is needed).

alt tag

Below is an example:

# github - twtrubiks
    Host github.com
    HostName github.com
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes
    User twtrubiks

# github - blue-rubiks
    Host blue.github.com
    HostName github.com
    IdentityFile ~/.ssh/id_rsa_rubik
    IdentitiesOnly yes
    User blue-rubiks

You can use the following command to test. If your own username is displayed, it means the setting is successful.

ssh -T git@github.com

alt tag

ssh -T git@blue.github.com

alt tag

ya 😍 we have set it up successfully~

If there is a problem, you can also use debug mode to see where the problem is. Usually, it is an error in the config input.

ssh -vT git@github.com

Next, let me explain to you first.

git config --global user.name "blue-rubiks"
git config --global user.email "blue555236sa56@gmail.com"

--global means global. If it is not entered, it means that the repo under the directory will take effect.

Now let's clone a repo to play with (you can create one yourself).

git clone git@github.com:blue-rubiks/github-ssh-test.git

Next, switch to the directory folder and enter the following command:

git config user.name "blue-rubiks"
git config user.email "blue555236sa56@gmail.com"

Note that --global is not added here, so it only takes effect for the repo under that directory.

We find the .git folder in the repo, and then find the config file. The content is roughly as follows:

alt tag

The main thing is to modify line 9 to line 10, changing it to the Host you set.

Finally, you can commit and push with peace of mind.

If you really don't understand what I'm talking about, I suggest watching the video explanation. I will walk you through the operation 😆

Execution Environment

  • Win 10

Reference

Donation

The articles are all original after my own research and internalization. If it has helped you and you want to encourage me, you are welcome to buy me a cup of coffee 😆

alt tag

Sponsor Payment

License

MIT license