preface

The problem most of us have is that our company has a Github account for work. We also have our own private Github account, working on our own projects and writing about things that interest us. But how do we switch from our company github to our own private Github account on our computer?

My company internally suggested using Smartgit to streamline git operations, but I couldn’t switch to my own account on it, which meant I had to use my own private account on my own computer. Oh, that’s a really annoying question.

After googling and a lot of trial and error, I finally managed to solve the problem. Now, let me tell you how this one works.

Essentially, it’s just a matter of balancing git and SSH configurations — which isn’t as bad as it looks. – Michael Herman

The operation process

Its operations include

  • Creating an SSH Key
  • Add the key to the Github account;
  • Create config files to manage individual keys
  • Update the stored key
  • Test Git Clone and Git push
  • How do I switch the Github account on a TERMINAL

1. Create an SSH key

In my case, I have two Github accounts, one for work and the username Yuanzhen-Kooboo; The other is private: Huangyuanzhen. So, I’ll create two keys, one for each account:

Operating as follows:

  • Open the CMD.
  • Enter the commands in sequence:
    cd ~/.ssh
    ssh-keygen -t rsa -C "1356409766@qq.com"
    ssh-keygen -t rsa -C "3083074260@qq.com"
Copy the code
  • When prompted “Enter file in which to save the key”, save the file as ID_rsa_ <>. In my example, I save the files as ~/.ssh/id_rsa_personal and ~/.ssh/id_rsa_company;

The effect is as follows:

Under C:\Users\huangyuanzhen\. SSH, you can see that the following four files are generated:

  • id_rsa_personal
  • id_rsa_personal.pub
  • id_rsa_company
  • id_rsa_company.pub

2. Bind the new key to the Github account

  • Open the id_rsa_personal. Pub file with Notepad and select and copy it all.
  • Go to my private Github account, find setting, open it, click “SSH and GPG keys” option, you can see an “Add SSH key” button, paste the content just copied into the text area, and add a related title; Here’s what it looks like after success:

  • In contrast, repeat the corresponding operation on other accounts; In my case, I paste id_rsa_company.pub into the SSH of my work account Yuanzhen-Kooboo.

3. Create a config file to manage keys

Create a config file in the ~/.ssh/ directory

    echo test>config
Copy the code

Find the file, open it with an editor (mine is vscode), then write the following to the file and save:

    # huangyuanzhen
    Host personal
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_personal
    
    # yuanzhen-kooboo
    Host company
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_work
Copy the code

Instead of naming our host github.com, we’ll name them personal and Company. The difference is that we now attach the new identity file we created earlier: ID_rsa_ <>;

4. Update the stored key

Before updating the storage, we need to check whether the local OpenSSH service is enabled. Or you’ll make a mistake.

The process for enabling the SSH service is as follows:

  1. Settings → Manage Optional functions → Add Functions → [OpenSSH Server]
  2. Computer management → services and applications → services → OpenSSH Authentication Agent&OpenSSH Server → right click

This is what it looks like when it starts:

Clear the current stored authentication:

    C:\Users\huangyuanzhen>ssh-add -D
    // All identities removed.
Copy the code

Added new keys:

    C:\Users\huangyuanzhen\.ssh>ssh-add id_rsa_company
    Identity added: id_rsa_company (id_rsa_company)
   
    C: \Users\huangyuanzhen\.ssh>ssh-add id_rsa_personal
    Identity added: id_rsa_personal (id_rsa_personal)
Copy the code

Check it out! Whether Github can recognize these keys; Enter in CMD:

     ssh -T personal
Copy the code

You can see “Hi Huangyuanzhen! You’ve successfully authenticated, but GitHub does not provide shell access. This shows that Github can recognize these keys. Cool !

5. Test Clone and push

Test the git clone

Take my personal account as an example, I want to clone the look-thinking warehouse on the Huangyuanzhen account locally and then operate it.

Enter on CMD:

    git clone git@personal:huangyuanzhen/Look-Thinking.git
Copy the code

You can see that the repository can be successfully cloned:

To operate the warehouse, run the command “CD look-thinking → code.

Test the git push

Take my personal Github account huangyuanzhen as an example. Create work-test repository on account; Then create the test folder locally:

    E:\mkdir test
    E: \cd test
    E: \test>echo test>readme.md
Copy the code

Once you’ve created the readme.md file, push it to Github;

    git init
    git add .
    git commit -am "first commit"
    git remote add origin git@personal:huangyuanzhen/test.git
    git push -u origin master
Copy the code

After successfully pushing the file, it looks like this:

You can see the results of the local operation on readme.md on your Github account. The same goes for Git pull.

How do we switch accounts using the command line?

Fatal: remote origin already exists. Fatal: remote origin already exists. After clearing the current connection, then connect to the warehouse in the new account, such as:

    git remote rm origin
    git remote git add origin git@company:yuanzhen-kooboo/work-test.git
Copy the code

Here, we disconnect the huangyuanzhen account and switch to The Yuanzhen-Kooboo account.

After this configuration, do not need to use smartgit and other auxiliary tools, directly in the terminal command line operation, I feel very convenient! Give it a try.

Note: The preceding command line is operated on Windows. If it is operated on other operating systems, replace it with the corresponding operation command.

data

  • Managing Multiple Github Accounts
  • Quick Tip: How to Work with GitHub and Multiple Accounts
  • Win10 OpenSSH