Add and manage multiple SSH-Keys for GitHub and Gitlab accounts at the same time

GitHub, GitLab, and Gitee (and recently an Alibaba-based codeup’ code repository) are currently the most popular code hosting platforms. The former is often used for personal code hosting, while the latter is often used for enterprise code hosting. Therefore, in actual work, we usually use these two platforms for code hosting on the same computer device.
SSH-Key is the key authentication method of these two platforms. Adding key authentication to your computer can complete the code hosting work more simply, efficiently and securely.
The following will explain in detail how to add different SSH-Keys for two platforms on the same computer and manage these keys.

Single user, single warehouse

  1. Register an account
    Use your own email address to register GitHub and Gitlab accounts, and ignore the registration process.
    image.png
    image.png
  2. Download Git
    Git installation package URL
    image.png
    Go to the website to download the version corresponding to your computer and install it directly.
    After the installation is complete, enter the following code in CMD to check the Git version number.
git -- version

image.png

  1. Generate SSH-Key
    Open GitBash (after Git is installed successfully, C will be automatically added to the right mouse button to add GitBash) and enter the following command to generate the key.
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/gitlab_id_rsa

ssh-keygen -o -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/gitlab_id_rsa
Both commands are used to generate SSH key pairs. Here is the meaning of the parameters used in each command:

1. `ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/gitlab_id_rsa`
   - `-t ed25519`: Specifies the algorithm type for generating key pairs as ed25519. ed25519 is an asymmetric encryption algorithm suitable for generating high-security key pairs.
   - `-C "[email protected]"`: Specifies a comment, usually used to identify the owner or purpose of the key pair. In this example, the annotation indicates that the email address is "[email protected]".
   - `-f ~/.ssh/gitlab_id_rsa`: Specify the file path and name of the generated key pair. In this example, the generated private key file is "~/.ssh/gitlab_id_rsa", and the public key file is automatically named "~/.ssh/gitlab_id_rsa.pub".

2. `ssh-keygen -o -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/gitlab_id_rsa`
   - `-o`: Specifies to output the generated key pair in OpenSSH format. The OpenSSH format supports additional features such as encryption algorithm selection and key expansion.
   - `-t rsa`: Specifies the algorithm type for generating key pairs as RSA. RSA is an asymmetric encryption algorithm and one of the most commonly used algorithms.
   - `-b 4096`: Specifies the number of digits in the key. 4096 means that the generated key length is 4096 bits. Generally speaking, the longer the key bits, the higher the security, but it also increases the computational burden.
   - `-C "[email protected]"`: The same meaning as the previous command, specify a comment, identify the owner or purpose of the key pair.
   - `-f ~/.ssh/gitlab_id_rsa`: The same meaning as the previous command, specify the file path and name of the generated key pair.

These parameters can be adjusted according to actual needs, and an SSH key pair suitable for your usage scenario can be generated according to your requirements.

image.png

  1. Add SSH-Key
    There are two ways to copy a key.

Enter cat ~/.ssh/ssh file name | clip, for example cat ~/.ssh/gitlab_id_rsa.pub | clip, and copy the ssh key.

Windows users directly use Notepad to open gitlab_id-rsa.pub and copy SSH-Key.
Linux/macos users can use cat or less to view
image.png
After copying the key of the corresponding platform, you can add the key to the SSH-Key of the corresponding platform.
image.png

  1. test key
    After adding the SSH-Key, we need to test whether the key is available. Taking GitHub as an example, enter the following command in GitBash.
ssh -T [email protected]

Under normal circumstances, a single user with a single warehouse will display basically the same output as me below. Mainly to see Hi xxxx! Youve successfully authenticated. . That's it. Why the command execution in the picture below is different from the one described above is because I have configured multi-warehouse and multi-account management. It will be introduced below. `
image.png
As shown in the figure, GitHub’s key authentication is successful. The GitLab key authentication test method is the same as above.

It should be noted that since Gitlab is a private code warehouse, you need to enter the IP address or domain name of Gitlab after git@ during testing in order to test correctly.

Multiple key management

When you generate multiple keys on the same computer, you need to authenticate and manage multiple keys, otherwise your keys will not work properly.
Specific steps are as follows.

Note: Registering and generating ssh-key is the same as for a single user

1 Add config file

First, manually configure the config file in the ./ssh/ folder to configure the key information.
image.png

image.png

#gitlab
Hostgitlab.com
HostName gitlab.com
User root
IdentityFile ~/.ssh/id_rsa_gitlib

#github
Hostgithub.com
HostName github.com
User root
IdentityFile ~/.ssh/id_rsa_github

#gitee
Hostgitee.com
HostName gitee.com
User root
IdentityFile ~/.ssh/id_rsa_gitee
Host represents keywords
HostName represents the host address
User represents user name
IdentityFile represents the authentication file

The config file is the management configuration file. Once you configure it, you can easily switch the git account and warehouse you use.

3 Help documentation

Address: https://help.gitee.com/base/account/SSH public key settings
gitee basically meets the needs of Chinese people and completely presents the required step introduction.
image.png

Multiple key authentication test

After completing the config configuration, you can use ssh -T git@ to test whether the keys of different platforms can be connected successfully.
GitHub test
image.png
Gitee test
image.png

Git common commands

Finally, the commonly used Git commands are attached for your reference.

  1. Remote warehouse related commands
Check out the repository: $ git clone [git url]
View the remote repository: $ git remote -v
Add a remote repository: $ git remote add [name] [url]
Delete the remote repository: $ git remote rm [name]
Modify the remote repository: $ git remote set-url --push[name][newUrl]
Pull the remote repository: $ git pull [remoteName] [localBranchName]
Push the remote repository: $ git push [remoteName] [localBranchName]

  1. Branch operation related commands
View local branch: $ git branch
View remote branch: $ git branch -r

Create a local branch: $ git branch [name] ----Note that the new branch will not automatically switch to the current branch after it is created
Switch branches: $ git checkout [name]
Create a new branch and immediately switch to the new branch: $ git checkout -b [name]

Create a remote branch (push the local branch to the remote): $ git push origin [name]
Merge branches: $ git merge [name] ----Merge the branch named [name] with the current branch

Delete local branch: $ git branch -d [name]
Delete remote branch: $ git push origin -d [name]

  1. Local project connects to remote warehouse
View the status of all files under the current project: $ git status

The (.) dot indicates that all the contents in the current directory are handed over to git management: $ git add .

Give a description of what you updated or modified: $ git commit –m”new natter”

Check which warehouse address your current project is connected to remotely: $ git remote -v

Submit the local project to the remote warehouse: $ git push origin master

Pull the remote warehouse project into the local project: $ git pull origin master

Some operations and classifications related to Git development

image.png