GitHub and GitLab are 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. So in practice, we usually use both platforms for code hosting on the same computer device.

Ssh-key is the Key authentication mode used by the two platforms. Adding Key authentication to your PC makes code hosting easier, more efficient, and more secure.

This article explains in detail how to add different SSH-keys for both platforms on the same machine and manage them.

Instance to explain

1. Set up an account

Use your own email to register GitHub and Gitlab accounts. The registration process is ignored.

2. Download Git

Git installation package url

Enter the website to download the corresponding version of your computer, directly install.

Once the installation is complete, type the following code into CMD to check the Git version number.

git -- version
Copy the code

3. Generate the SSH Key

Start GitBash and run 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 [email protected] indicates the mailbox used to register the Gitlab account-fSSH/Indicates the SSH file path. Gitlab_id -rsa indicates the SSH file name (you can customize it).Copy the code

At this point, we have generated a Gitlab key, and GitHub key is generated in the same way as above.

4. Add the SSH Key

There are two ways to copy a key.

  1. Input the cat ~ /. SSH/SSH file name | clip, such as the cat ~ /. SSH/gitlab_id_rsa pub | clip, copy the SSH key.

  2. Open gitlabid-rsa. pub with Notepad and copy the ssh-key.

After the Key of the platform is copied, you can add the Key to the sSH-key of the platform

5. Test the key

After adding the ssh-key, we need to test whether the Key is available. Using GitHub as an example, type the following command in GitBash.

ssh -T [email protected]
Copy the code

As shown in the figure, GitHub’s key authentication is successful, and Gitlab’s key authentication test method is the same as above.

Note that since Gitlab is a private code repository, you need to enter the IP address or domain name of Gitlab after git@ to test it correctly.

6. Manage multiple keys

When you generate multiple keys on the same computer, you need to manage authentication for multiple keys, otherwise your keys will not work properly.

The specific steps are as follows.

6.1 Adding the Config File

First manually configure the key information in the config file under the./ SSH/folder.

# gitlab
Host gitlab.com
HostName gitlab.com
User root
IdentityFile ~/.ssh/id_rsa

# githubHost github.com HostName github.com User root IdentityFile ~/. SSH /id_rsa_github Host Indicates the keyword. HostName indicates the Host address IdentityFile indicates the authentication fileCopy the code

6.2 Adding A Key Trust

To complete the configuration and add trust to each key, open Git Bash and type the following command.

eval $(ssh-agent -s)

ssh-add ~/.ssh/other_id_rsa

evalSsh-add Indicates that SSH file authentication is addedCopy the code

It is important to remind that the config file is mainly used to manage the key information and has no actual function. It is also possible to add trust for the key directly without adding the config file.

However, considering key information management, you are strongly advised to add a config file to configure key information.

6.3 GITLab official example

7. Multi-key authentication test

After multi-key authentication is complete, run the SSH -t git@ command to test whether the keys of different platforms can be successfully connected.

7.1 making

7.2 Gitlab

Git Git

Finally, the common Git command is attached for your reference

1. Commands related to the remote warehouse

Check out the repository: $gitclone[git url] $git remote add [name] [url] $git remote rm [name] $git remoteset$git pull [remoteName] [localBranchName: $git push [remoteName] [localBranchName]
Copy the code

2. Branch commands related to operations

$git branch -r Create a local branch: $git branch [name] ---- $git checkout -b [name] : $git push origin [name] : $git push origin $git merge [name] ---- Merge the branch named [name] with the current branch-d[name] : $git push origin-d [name]
Copy the code

3. Connect the local project to the remote repository

$git status (.) $git add. $git add. $git remote -v ($git remote) $git commit ($git remote) $git pull Origin Master To pull the remote repository project to the local project: $git pull Origin MasterCopy the code

conclusion

That’s all for this article, thank you for reading, if you have any questions or suggestions, feel free to leave a comment and discuss with each other.

Finally, I wish you all the best in your work and a happy life.