Git User Manual: differences between HTTPS and SSH

Source: Internet
Author: User

Git User Manual: differences between HTTPS and SSH

In managing Git projects, https URLs are often directly used to clone to the local machine. Of course, some people also use SSH URLs to clone to the local machine. The main difference between the two methods is that using https url cloning is more convenient for beginners. just copy the https url and then directly clone it to the local machine using the clone command in git Bash, however, every time you run fetch and push code, you need to enter the account and password, which is also a headache for https. To clone an SSH url, you must configure and add an SSH key before cloning. Therefore, if you want to clone an SSH url, you must be the owner of the project. Otherwise, you cannot add an SSH key. By default, no account or password is required for each fetch or push code, if you want to enter the account password each time for fetch and push, you can also set it. The previous articles introduced the use of https in Git blogs as a case. Today, we mainly record how to configure the use of ssh to submit and clone code.

Step 1: Check whether your computer has generated an SSH Key
Run the following command in git bash:

cd  ~/.ssh ls

The two commands check whether the id_rsa.pub or id_dsa.pub file already exists. If the file already exists, skip step 2 and go directly to step 3.

Step 2: Create an SSH Key
Run the following command in git bash:

Ssh-keygen-t rsa-C "your email address"

Description of code parameters:
-T specifies the key type. The default value is rsa, which can be omitted.
-C: Set the comment text, such as the mailbox.
-F specifies the key file storage file name.
The above command omitted the-f parameter. Therefore, after running the above command, you will be asked to enter a file name to save the generated SSH key code, such:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/you/. ssh/id_rsa): [Press enter]
Of course, you can also use the default file name (recommended) without entering the file name. Then, two key files, id_rsa and id_rsa.pub, will be generated.
Then you will be prompted to enter the password twice (the password is required when you push the file, rather than the password of the github administrator). Of course, you can also do not enter the password, press enter directly. When pushing, you do not need to enter the password and submit it directly to the Git server, for example:
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Next, the following prompts will be displayed, such:
Your identification has been saved in/c/Users/you/. ssh/id_rsa.
Your public key has been saved in/c/Users/you/. ssh/id_rsa.pub.
The key fingerprint is:
01: 0f: f4: 3b: ca: 85: d6: 17: a1: 7d: f0: 68: 9d: f0: a2: db your email address
When you see this section in Git Bash, it indicates that your SSH key has been successfully created and you only need to add it to the SSH key on the Git server.

Step 3: add an SSH Key to the Git Server
This is because different Git servers have different regions added on the interface, so we will not detail them here. For details, refer to the operations of different Git servers.
After the above three steps, you can use the ssh url method to submit and clone the project code, and do not need to enter the account password frequently, if you have been using https for development and want to switch to ssh for development, you only need to perform the following steps:

Git remote rm origin git remote add origin "ssh format address of Git repository" git push origin

In managing Git projects, https URLs are often directly used to clone to the local machine. Of course, some people also use SSH URLs to clone to the local machine. The main difference between the two methods is that using https url cloning is more convenient for beginners. just copy the https url and then directly clone it to the local machine using the clone command in git Bash, however, every time you run fetch and push code, you need to enter the account and password, which is also a headache for https. To clone an SSH url, you must configure and add an SSH key before cloning. Therefore, if you want to clone an SSH url, you must be the owner of the project. Otherwise, you cannot add an SSH key. By default, no account or password is required for each fetch or push code, if you want to enter the account password each time for fetch and push, you can also set it. The previous articles introduced the use of https in Git blogs as a case. Today, we mainly record how to configure the use of ssh to submit and clone code.

Step 1: Check whether your computer has generated an SSH Key
Run the following command in git bash:

cd  ~/.ssh ls

The two commands check whether the id_rsa.pub or id_dsa.pub file already exists. If the file already exists, skip step 2 and go directly to step 3.

Step 2: Create an SSH Key
Run the following command in git bash:

Ssh-keygen-t rsa-C "your email address"

Description of code parameters:
-T specifies the key type. The default value is rsa, which can be omitted.
-C: Set the comment text, such as the mailbox.
-F specifies the key file storage file name.
The above command omitted the-f parameter. Therefore, after running the above command, you will be asked to enter a file name to save the generated SSH key code, such:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/you/. ssh/id_rsa): [Press enter]
Of course, you can also use the default file name (recommended) without entering the file name. Then, two key files, id_rsa and id_rsa.pub, will be generated.
Then you will be prompted to enter the password twice (the password is required when you push the file, rather than the password of the github administrator). Of course, you can also do not enter the password, press enter directly. When pushing, you do not need to enter the password and submit it directly to the Git server, for example:
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Next, the following prompts will be displayed, such:
Your identification has been saved in/c/Users/you/. ssh/id_rsa.
Your public key has been saved in/c/Users/you/. ssh/id_rsa.pub.
The key fingerprint is:
01: 0f: f4: 3b: ca: 85: d6: 17: a1: 7d: f0: 68: 9d: f0: a2: db your email address
When you see this section in Git Bash, it indicates that your SSH key has been successfully created and you only need to add it to the SSH key on the Git server.

Step 3: add an SSH Key to the Git Server
This is because different Git servers have different regions added on the interface, so we will not detail them here. For details, refer to the operations of different Git servers.
After the above three steps, you can use the ssh url method to submit and clone the project code, and do not need to enter the account password frequently, if you have been using https for development and want to switch to ssh for development, you only need to perform the following steps:

Git remote rm origin git remote add origin "ssh format address of Git repository" git push origin

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.