How do I use multiple Github accounts on one computer

Scenario: Multiple Github accounts are used on a PC

For example, the person account of personal Github and the Work account of company Github cannot be used to operate the Git repository under person’s cross-account.

When I use the Work account to submit the code belonging to the Person account, the following error will appear, indicating that I have no permission.

$ git push origin master ERROR: Permission to person/git-start.git denied to work. fatal: Could not read from remote repository.

Run the git remote -v command to view the remote Git address of the repository

$ git remote -v origin [email protected]:person/git-start.git (fetch) origin [email protected]:person/git-start.git (push)

So how do you manage multiple Github accounts on one computer?

1. Set the SSH key

Create multiple SSH keys and save them in the corresponding files

cd ~/.ssh

ssh-keygen -t rsa -C “[email protected]

ssh-keygen -t rsa -C “[email protected]

.

Pub and id_rsa_person, id_rsa_person.pub

2. Add the SSH key to the Github account

Copy the key to the clipboard

pbcopy <~/.ssh/id_rsa_work.pub

Add the contents of the public key content (that is, the.pub file) from the generated key to the different Github accounts

  • Go to github account Settings

  • Click “SSH Key”, then “Add SSH Key”

  • Paste the key into the Key field and add a title

  • Click “Add Key” and enter your Github password to confirm

3. Create a Config configuration file to manage the keys separately

$ cd ~/.ssh/

$ sudo vim config

Edit config file

# work
Host work
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work

# personal
Host personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal
Copy the code

Adding a new key

$ ssh-add id_rsa_work
$ ssh-add id_rsa_personal
Copy the code

View the current key list and check whether the key is added successfully

$ ssh-add l
Copy the code

Tests to ensure that Github recognizes the key:

$ ssh -T work
Hi work! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T personal
Hi personal! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code

Have a try

There is one more thing to be careful of when interacting with remote libraries.

Back on the command line, create a test directory:

$ cd ~/documents
$ mkdir git-start
$ cd git-start
Copy the code

Using the Work account, add a blank “readme.md” file and PUSH to Github:

$ touch readme.md
$ git init
$ git add .
$ git commit -am "first commit"
$ git remote add origin git@work:work/git-start.git
$ git push origin master
Copy the code

Notice how we use the custom account git@work instead of [email protected]!

Also, git@work:work/git-start.git. The first git is the Host created in the config file, and the second git is your Github username.

Try the PUSH and PULL operations on Person again

Tips, change remote warehouse command

$ git remote set-url origin git@work:work/git-start.git
Copy the code

In summary, the core of managing multiple Github accounts on one computer is this

  • SSH key
  • Config File Configuration
  • Git repository remote address configuration

In this case, there are a few caveats

1. There was a problem in remote pull push, because the mailbox was to be set. The mailbox was identified in pull, two Github accounts and two mailboxes, so we naturally could not use the global user.email

Git config --global --unset user.email Git config user.email for each project repo"[email protected]"
git config  user.name "suzie"
Copy the code

And then there’s no problem with push pull

Git global user.email: git global user.email: git global user. God damn it, it’s too dangerous

For example, my account correspondence is

Git config user.email “[email protected]” will be submitted using the AshinGit account by default

About git config configured in the user’s priority, local > global > system, see https://blog.csdn.net/trochiluses/article/details/8921138

What’s the best way to do that?

  • Global global holds users for common submissions
  • A local repO holds users that need special commits