The background,
At present, many companies choose Git as the code version control tool, and then set up their own private GitLab to manage the code. We can choose HTTP protocol when clone code, of course, we can also choose SSH protocol to pull the code. However, it is rarely found on the Internet how to use git client to generate SSH keys and configure them in GitLab. At that time, I was struggling with groping, and finally found a solution. So in this article, we will talk about how to generate SSH keys from local Git client and configure them in GitLab. Then use SSH protocol to submit and pull git remote repository code.
Second, solutions
-
Open local Git bash and run the following command to generate SSH public and private key pairs
Ssh-keygen -t rsa -c ‘[email protected]’ then press Enter (the -c parameter is your email address)
-
Then open the ~/.ssh/id_rsa.pub file (~ indicates the user directory, for example C:\Users\Administrator on my Windows) and copy the contents
-
Open GitLab, go to Profile Settings–>SSH Keys– >Add SSH Key, paste the content copied in the previous step into the text box corresponding to Key, set a name for this sshkey in the text box corresponding to Title, click Add Key button
4. Now that all steps of gitLab’s SSH key configuration are completed, we can happily use SSH protocol for code pulling and submission operations
5. Try the pull and submit codes again, and there should be no need to enter a password
3. Configure multiple SSH keys locally
Most of the time, there are many git hosts on the machine, such as gitLab, Github, osChina, etc., so we need to configure multiple SSH keys locally. To enable different hosts to use different SSH keys, do the following (gitLab and Github as examples) :
-
Generate a pair of SSH keys for the company
ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab-rsa Copy the code
-
Generate a pair of SSH keys for Github
ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github-rsa Copy the code
-
In the ~/. SSH directory, create a file named config (without the suffix). SSH keys are used to configure multiple hosts using different SSH keys as follows:
# gitlab Host gitlab.com HostName gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab_id-rsa # Github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/. SSH /github_id-rsa # # HostName: the name of the Host you want to log in to # User: the login name # IdentityFile: Specifies the identityFile path corresponding to the User aboveCopy the code
-
Add the generated public keys gitlabid-rsa. pub and githubid-rsa. pub to Gitlab and Github respectively
-
Git permission verification is not required again.
-
Check again ~/.. Pub and github_id-rsa and github_id-rsa.pub
Third, summary
Why do we use the SSH protocol? Because SSH is more secure and convenient, for example, our company’s Gitlab has changed from HTTP protocol to HTTPS protocol, so if you use HTTP protocol to manage code, you need to change the git address in all projects to HTTPS, which will drive us crazy! But if we start with SSH, we don’t need to change anything.
Note: the file from www.cnblogs.com/hafiz/p/814…
Git sets global and individual repository accounts and passwords
Git global configuration and username mailbox configuration for a single repository
-
Configure the account and password for the global repository
Git config --global user.email "email address" git config --global user.email "email addressCopy the code
-
If your company project is also in Git, in order not to conflict with your personal account, you need to set up a separate account and password for the repository to enter the repository, using the following command
Git config user.name "userName" git config user.email "email address" // your email addressCopy the code
-
After setting the account and password
Git config --listCopy the code
www.cnblogs.com/qqcc1388/p/…
Git configures multiple Ssh-keys
background
When you have multiple Git accounts, for example:
A. A Gitee for in-house work development; B. A Github for some of your own development activities;
The solution
- Generate an ssh-key for the company
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitee_id_rsa
Copy the code
- Generate a github ssh-key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github_id_rsa
Copy the code
- In the ~/. SSH directory, create a config file and add the following contents: Host and HostName specifies the git server domain name, and IdentityFile specifies the path to the private key.
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
Copy the code
4. Run SSH commands to test them respectively
$ ssh -T [email protected]
$ ssh -T [email protected]
Copy the code
Using Gitee as an example, the following figure is returned on success
Gitee.com/help/articl…