A detailed introduction to the clone method of https and SSH in git and the SSH conflict resolution of multiple github accounts and the problem that the SSH port is blocked by the firewall Solve the problem of cloning private projects through HTTPS

learning target:

Master the knowledge of Git


Learning Content:

1. The difference between HTTPS and SSH 2. Steps for complete configuration of SSH keys and public keys3. SSH conflict resolution for multiple github accounts4. SSH ports blocked by firewalls

study-time:

2022.2.10

Learning output:

1. The difference between HTTPS and SSH

There are two ways to clone a project in git: HTTPS and SSH. The differences between them are as follows:

HTTPS: No matter who you are, you can clone the url at will, but you need to verify the username and password when pushing;
SSH: You must be the owner or administrator of the clone project, and you need to add an SSH Key before the clone.
When SSH pushes, you do not need to enter the user name. If a password is set when configuring the SSH key, you need to enter the password. Otherwise, you do not need to enter the password directly.

Steps to use SSH Key in git: Check whether the SSH Key exists on the computer: $ cd ~/.ssh$ ls If there is an id_rsa.pub or
id_dsa.pub file, it means that the file exists and skips the step of creating an SSH Key. Create SSH Key to put public SSH on remote repository

2. Completely configure the steps of SSH key and public key

Please see my other article in this column

3. SSH conflict resolution for multiple github accounts

github uses SSH to connect with the client. If it is a single user (first), after generating the key pair, save the public key to github, and
the SSH client sends the local private key (default ~/.ssh/id_rsa) to the server for verification each time it connects.
In the case of a single user, the public key stored on the connected server and the private key sent are naturally paired.

But if it is multi-user (first, second), when we connect to second's account,
second saves its own public key, but the SSH client still sends the default private key, which is first's private key, then this verification is naturally impossible. pass through.
However, to achieve SSH key switching under multiple accounts, you can do some configuration on the client side.

First, cd to ~/.ssh and use ssh-keygen -t -rsa -C '[email protected]' to generate a new SSH key: id_rsa_second. After generating, add the new SSH public key to github.

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

By default SSH will only read id_rsa, so in order for SSH to recognize the new private key, it needs to be added to the SSH agent

ssh-add/.ssh/id_rsa_second

If the command reports an error: Could not open a connection to your authentication agent. Unable to connect to the ssh agent, execute ssh-agent bashthe command and then execute the ssh-addcommand.

After completing the above steps, create a config file in the ~/.ssh directory, which is used to configure the server corresponding to the private key. The content is as follows:

 Default github user(first@mail.com)
Host github.com
HostName github.com
User git
IdentityFile C:/Users/username/.ssh/id_rsa

#second user([email protected])
Host github-second
HostName github.com
User git
IdentityFile C:/Users/username/.ssh/id_rsa_second

After the configuration is complete, when connecting to a github repository with a non-default account, the address of the remote repository needs to be modified accordingly. For example, when adding a repository test under the second account, you need to add it like this:

git remote add test git@github-second:second/test.git 
#并非原来的[email protected]:second/test.git

This way each connection will use id_rsa_second to connect to the server. So far, you're done!

Note: github obtains the github account and displays author information according to the user.email of the configuration file, so for users with multiple accounts, you must remember to change user.email to the corresponding email ([email protected]).

4. Solve the problem that the SSH port is blocked by the firewall

Set SSH to use HTTPS port 403
In the LAN, SSH port 22 may be blocked by the firewall, you can set SSH to use HTTPS port 403.

Test if the HTTPS port is available

$ ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

Edit the SSH configuration file ~/.ssh/config as follows:

Host github.com
Hostname ssh.github.com
Port 443

Test whether the configuration is successful

$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

5. Clone private projects via HTTPS or SSH

At present, github's private projects have been available for free for a period of time, but when you use the ordinary clone method to download to the local, you will find that the clone fails and an error message is displayed.

remote: Repository not found.
fatal: repository ‘https://github.com/github-username/github-template-name.git’ not found

Normally, the link format of clone is generally

https://github.com/github-username/github-template-name.git

Then when you want to clone your own private project locally, for the security of the private project, you need to add an account and password for verification

git clone https://github-username:github-password@github.com/github-username/github-template-name.git

When cloning, add github-username:github-password after https://, and then the @project link can clone the project normally. This is a relatively simple way to clone your own private project.

Some graphical Git software can prompt you to enter an account and password, which is more convenient. For example
insert image description here
, enter the HTTPS link of the private warehouse, click clone, it will automatically pop up the input box, allowing you to enter the account password, which is more user-friendly

However, SourceTree, the software
insert image description here
, cannot clone by entering HTTPS links. Because this is a private repository
, the solution is to log in to the remote account in SourceTree
insert image description here
to pull the private repository

Alternatively, using an SSH link can also meet the requirements. Because SSH matches the private key and the public key, there is no need to enter an account and password

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122854653