1, clear the default global username and email (ignore them if there are none)

  • View the configured Git list
git config --list
Copy the code
  • Clear the default user name and mailbox
git config --global --unset user.name
git config --global --unset user.email
Copy the code

Generate ssh-keys for different git accounts: a corporate account, a personal account

  • Git generates ssh-key, which is id_rsa by default if the name is not set
// Press Enter ssh-keygen -t rsa -c "[email protected]"Copy the code

If the following information is displayed, the key is successfully generated

Generating public/private rsa key pair. Enter file in which to save the key (/Users/james/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/james/.ssh/id_rsa. Your public key has been saved in /Users/james/.ssh/id_rsa.pub. The key fingerprint is: SHA256:rwtxjGTJPoV9Mg8lFSf8D4X6jFexWVXKOMRaVyo+RO8 [email protected] The key's randomart image is: +---[RSA 3072]----+ | .o=o+. .*| | . + o.=+++o.| | * * .==o== | | + + *ooo++ | | = S .+o+E | | + .. +.. | |... | | . . | | o. | +----[SHA256]-----+Copy the code
  • Your own Git generates ssh-key and sets the path to [email protected], as opposed to the one generated earlier
Ssh-keygen -t rsa -f ~/. SSH /[email protected] -c "[email protected]"Copy the code

3. Add them to the SSH-Agent trust list

  • Add to the trust list
ssh-add ~/.ssh/id_rsa
Copy the code
  • You might get an Error like this
Could not open a connection to your authentication agent.
Copy the code
  • Enter it first
ssh-agent bash
Copy the code
  • Repeat 3.1 and return Identity Added
Ssh-add ~/. SSH /id_rsa Identitiy added: ~/. SSH /id_rsaCopy the code
  • Add another one to the trust list again
SSH -add ~/. SSH /[email protected] //Copy the code

4. Add the public key to the Git account

  • Copy the public key and paste it into the public key on your git website
pbcopy < ~/.ssh/id_rsa.pub
//pbcopy < ~/.ssh/[email protected]
Copy the code

5. Configure multiple SSH-keys in the config file

  • 5.1. Open the directory and see if there is a config file. If not, create a new one
open ~/.ssh/
Copy the code
  • 5.2. Configure sSH-keys for the company and yourself
key value The rules
Host The host I’m just going to do whatever I want. I’m just going to make a connection
Hostname The host name It must be written correctly to your Git public address, such as gitee.com
IdentityFile Identity documents Your RSA specific path address
User The user It is recommended to use the first part of Host. The user will be used for clone operations later

The content of the config file is as follows:

#gmail
Host gmail.github.com
Hostname github.com
IdentityFile ~/.ssh/[email protected]
User gmail
  
#126
Host 126.github.com
Hostname github.com
IdentityFile ~/.ssh/[email protected]
User 126
Copy the code

6. Test the connection

  • SSH -t -t git@{config}. XXX host name
ssh -T [email protected]
Copy the code
  • The connection is successful
Hi xxx! You've successfully authenticated.but GitHub does not provide shell acess
Copy the code

  • Test your own and connect successfully
SSH -t [email protected] Hi XXX! You've successfully authenticated.but GitHub.COM does not provide shell acessCopy the code

At this point, you’re basically having fun

Clone and Push

// Go to the directory where you work, To download a project down CD ~ / James/demo / / / the situation of the originally single account git clone [email protected]: XXX/TestAndroid git / / set up multiple accounts and git clone after config [email protected]:xxx/demo.git //..... Git add. Git pull git commit -m "test commit" git push //Copy the code