GitHub-Microsoft-github.com
Gitee-Open Source China-gitee.com
Gitlab-Ukrainian GitLab company-gitlab.com
JihuLab – Gitlab operated by Chinese agents – gitlab.cn or jihulab.com
Using an SSH public key allows you to use a more secure connection when communicating between your computer and platforms such as GitHub (Git Remote
needs to use SSH address
, such as [email protected]:king/example.git
), but the speed is not as fast as HTTPS
( For example https://gitee.com/king/example.git
) is fast. Today, let’s learn the detailed process of using secure connection configuration
when using SSH public key to communicate between the computer and GitHub and other platforms.
Latest article (2023-9-19 20:58:21)
: Solving ssh:connect to host github.com port 22: Connection timed out and kex_exchange_identification
1. Generate SSH public and private keys
1.1 Cancel global settings
If you have set it directly in TortoiseGit or used the following command to set global user.name and user.email.
$ git config --global user.name "your name" $ git config --global user.email "your email"
You can use the following command to clear global settings [Recommended]
$ git config --global --unset user.name "your name" $ git config --global --unset user.email "your email"
Use the git config –global –list command to check whether you clear global settings successfully: If user and email do not appear, it means < strong>Global settings cleared successfully.
1.2 Generate SSH public and private keys
After specifying the private key name in the ssh-keygen command, a pair of SSH public key and private key will be generated at the same time, with the .pub suffix. is the public key.
For example, gitee_id_rsa is the private key, and gitee_id_rsa.pub is the public key.
1. The location of the private key gitee_id_rsa is configured in the config file. After the developer manually executes the ssh-keygen command to generate it, it is stored in the ~/.ssh directory of the local machine. [C:\Users\username\.ssh] directory. The private key is the user’s ID card, which uniquely identifies the user. Keep it carefully and do not disclose it.
2. Configure the public key gitee_id_rsa.pub to the corresponding code hosting platform (such as GitHub & amp;Gitee & amp;Gitlab & amp;JihuLab) middle.
3. When submitting code, confirm the user identity and submitted platform by verifying the corresponding SSH public and private keys.
Generate SSH public and private keys
[The email address [email protected] here is not the default and needs to be modified to the developer’s personal configuration] 1. Generate an SSH-Key for Gitee. ssh-keygen -t rsa -C [email protected]’ -f ~/.ssh/gitee_id_rsa 2. Generate an SSH-Key for GitHub. ssh-keygen -t rsa -C [email protected]’ -f ~/.ssh/github_id_rsa 3. Generate an SSH-Key for GitLab. ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab_en_id_rsa 4. Generate an SSH-Key for JihuLab ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab_id_rsa Note that after entering the command to generate SSH-key, you need to press Enter three times, which means empty, which means "empty". "Empty" means no password.
The rsa in the ssh-keygen command is an algorithm for generating keys. You can also change other algorithms according to your needs >To generate ssh key.
For example ssh-keygen -t ed25519 -C "[email protected]" The ed25519 algorithm is used here. Then go to the corresponding platform and re-add the public key
1.3 Screenshot of the process of generating GitLab’s SSH public and private keys (for other platforms, just replace the ssh-keygen command)
1.4 Contents of private key gitee_id_rsa and public key gitee_id_rsa.pub
Private key gitee_id_rsa
Public key gitee_id_rsa.pub
1.5 GitHub & amp;Gitee & amp;Gitlab & amp;JihuLab’s last generated SSH public and private keys
In the same way, GitHub, Gitee, and JihuLab repeat the same process as above. Replace the ssh-keygen command of the corresponding platform. Nothing here Again.
For the corresponding process between GitHub and Gitee, see my article: Git configures two different SSH-Keys for Gitee and GitHub at the same time.
2. Specify the config private key configuration file of GitHub &Gitee&Gitlab &Jihu
Because GitHub
& amp;Gitee
& amp;Gitlab
& amp;JihuLab
are configured at the same time. code>’s SSH public and private keys
, so it is different from configuring only one platform separately. In order to avoid confusing the SSH public and private keys of different platforms
, it needs to be in ~/. ssh
Create a new config
file under the directory [i.e. C:\Users\username\.ssh directory]
and add the following content (where Host
and HostName
fill in the domain name of the git server
, and IdentityFile
specifies the path to the private key
)
When configuring in config
, if no port is specified, default port 22
will be used for SSH connection, or can be modified to port 443
, The # symbol means comment
and has no practical effect. It is easy to read and understand
.
Note
: The Hostname
of GitHub port 443 is ssh.github.com
, not github.com
.
#github # ssh -T [email protected] Port 443 Hostname ssh.github.com Hostgithub.com Hostname ssh.github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github_id_rsa
Here is my config file
. When referring to it, you need to make some modifications
based on the actual configuration of your personal machine, such as private key location name. IdentityFile
.
#github # ssh -T [email protected] #Port 443 #Hostname ssh.github.com User git #Port 22 Comment here, port 22 is also used by default Hostgithub.com Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github_id_rsa #2023-3-3 00:57:06 #gitee #ssh-keygen -t rsa -C [email protected]’ -f ~/.ssh/gitee_id_rsa # ssh -T [email protected] Hostgitee.com Hostname gitee.com PreferredAuthentications publickey IdentityFile ~/.ssh/gitee_id_rsa # polar fox the first kind #2023-4-5 22:37:31 jihulab #ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab_id_rsa #ssh -T [email protected] Hostgitlab.cn Hostname altssh.jihulab.com User git Port 443 PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab_id_rsa # polar fox the second kind #2023-4-5 22:37:31 jihulab #ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab_id_rsa #ssh -T [email protected] Hostjihulab.com Hostname altssh.jihulab.com User git Port 443 PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab_id_rsa #2023-4-6 01:00:05 #ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab_en_id_rsa #ssh -T [email protected] #gitlab Hostgitlab.com Hostname gitlab.com #User git #Port 443 PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab_en_id_rsa
3. Add the corresponding public key to GitHub & Gitee & Gitlab & JihuLab. (You will be asked to enter a password for authentication when adding)
For the process of adding the corresponding public keys for GitHub and Gitee, see my article: Git configures two different SSH-Keys for Gitee and GitHub at the same time 2. Generate SSH public key–4. Add the corresponding public key to Gitee and GitHub. (You will be asked to enter a password for verification when adding)
3.1 Configuring the public key gitee_id_rsa.pub in Gitee
//View the generated Gitee public key gitee_id_rsa.pub and copy the Gitee public key gitee_id_rsa.pub //Or directly go to the C:\Users\username\.ssh directory and use a text editor to open the Gitee public key gitee_id_rsa.pub and copy the contents of the Gitee public key gitee_id_rsa.pub cat ~/.ssh/gitee_id_rsa.pub
Copy the Gitee public key gitee_id_rsa.pub:
Click Account Settings
Add title and Gitee public key gitee_id_rsa.pub content, click OK, and then enter password verification:
Added successfully:
3.2 Configuring the public key github_id_rsa.pub in GitHub
//View the generated GitHub public key github_id_rsa.pub, copy the GitHub public key github_id_rsa.pub cat ~/.ssh/github_id_rsa.pub
Click on Settings
Click New SSH Key
Enter the title & amp; Copy and paste the contents of the public key github_id_rsa.pub & amp; Click Add SSH Key
3.3 Configuring the public key gitlab_en_id_rsa.pub in Gitlab
//View the generated Gitlab public key gitlab_en_id_rsa.pub, copy the Gitlab public key gitlab_en_id_rsa.pub cat ~/.ssh/gitlab_en_id_rsa.pub
Click Preferences
Click SSH Keys
Click Add new key
After filling in the relevant information of Gitlab public key gitlab_en_id_rsa.pub click Add key:
3.4 Configuring the public key gitlab_id_rsa.pub in JihuLab
//View the generated JihuLab public key gitlab_id_rsa.pub, and copy the JihuLab public key gitlab_id_rsa.pub cat ~/.ssh/gitlab_id_rsa.pub
Click Preferences
Click SSH Key
Click to add a new key
After filling in the key – title – usage type – expiration time and other information – click Add Key
4. Check whether the configuration is successful
4.1 Detection command
When using it for the first time, enter yes< when Are you sure you want to continue connecting (yes/no/[fingerprint] ? appears) /strong>. [Indicates confirmation to add the host to the trusted list]
//Detect GitHub ssh -T [email protected] //Detect gitee ssh -T [email protected] //Detect JihuLab [Gitlab operated by Chinese agents] ssh -T [email protected] or ssh -T [email protected] //Detect Gitlab ssh -T [email protected]
4.2 Screenshot of successful execution of detection command
5. Git SSH and HTTPS command line introductory tutorial
Note: SSH [email protected]:djcking/myapplication01.git
and HTTPS https://gitee.com/djcking/myapplication01.git
Click to copy and paste directly from the warehouse to use. Whichever way is configured, just copy and use it.
5.1 SSH (used in conjunction with the above tutorial)
Git global settings
git config --global user.name "GiHub username" git config --global user.email "GitHub mailbox"
a: There is no git repository yet, create a git repository (here, take the myapplication01 repository as an example): //1-Create myapplication01 warehouse mkdir myapplication01 //2-Enter myapplication01 cd myapplication01 //3-Initialize the warehouse git init //3-Create README.md file touch README.md //4-add README.md git add README.md //5-commit git commit -m "first commit" //6-Bind remote warehouse git remote add origin [email protected]:djcking/myapplication01.git //7-Push to the master branch of the remote origin git push -u origin "master" or git push -u origin main [Because GitHub’s previous default branch has always been master, but after October 2020, GitHub announced that the default branch for new projects will be master is gradually changed to main. 】 b: Existing git repository: //1-Enter the existing git warehouse (here, take the myapplication01 warehouse as an example) //cd existing_git_repo cd myapplication01 //2-Bind the remote warehouse git remote add origin [email protected]:djcking/myapplication01.git 3-Push to the master branch of the remote origin git push -u origin "master" or git push -u origin main [Because GitHub’s previous default branch has always been master, but after October 2020, GitHub announced that the default branch for new projects will be master is gradually changed to main. 】
Easier usage
: Directly build the remote warehouse
, clone it locally
, without
initialization (git init ) and binding repository (git remote add origin), which can be used directly.
5.2 HTTPS (Git needs to be configured in HTTPS mode)
Git global settings
git config --global user.name "GiHub username" git config --global user.email "GitHub mailbox"
a: There is no git repository yet, create a git repository (here, take the myapplication01 repository as an example): //1-Create myapplication01 warehouse mkdir myapplication01 //2-Enter myapplication01 cd myapplication01 //3-Initialize the warehouse git init //3-Create README.md file touch README.md //4-add README.md git add README.md //5-commit git commit -m "first commit" //6-Bind remote warehouse git remote add origin https://gitee.com/djcking/myapplication01.git //7-Push to the master branch of the remote origin git push -u origin "master" or git push -u origin main [Because GitHub’s previous default branch has always been master, but after October 2020, GitHub announced that the default branch for new projects will be master is gradually changed to main. 】 b: Existing git repository: //1-Enter the existing git warehouse (here, take the myapplication01 warehouse as an example) //cd existing_git_repo cd myapplication01 //2-Bind the remote warehouse git remote add origin https://gitee.com/djcking/myapplication01.git //3-Push to the master branch of the remote origin git push -u origin "master" or git push -u origin main [Because GitHub’s previous default branch has always been master, but after October 2020, GitHub announced that the default branch for new projects will be master is gradually changed to main. 】
Easier usage
: Directly build the remote warehouse
, clone it locally
, without
initialization (git init ) and binding repository (git remote add origin), which can be used directly.
6. Add file README.md (SSH) using Git command line method
Gitee repository MyApplication that did not push README.md
Screenshot of the command after pushing
README.md
to Gitee repository MyApplication
After pushing README.md
to Gitee repository MyApplication
6.1 Before creating README.md
6.2 touch README.md
6.3 git add README.md
The git add command can add the modifications to the file to the staging area. By running the git add command, you tell Git which file changes should be included in the next commit. Add the file from the workdir (working directory) to the cache area index (i.e. the temporary storage area stage) (a red exclamation mark appears) //* represents all files git add* //.Represents all files in the current directory git add . //You can also add directories directly git add directory name //Add one or more files git add file 1 name file 2 name //Force a file to be submitted (regardless of whether .gitignore contains this file) //.gitignore file is a file used to configure to ignore files that do not need to be uploaded. git add -f filename
6.4 git commit -m “add README.md”
git commit - m
“Code submission information
”
Code submission information
is equivalent to comments in code
.
After execution, a unique 40-digit submission ID string will be generated based on the digest algorithm (SHA1)
. By default, only 7 digits will be displayed. uniquely identifies you. Every commit
, here is 731adec
.
6.5 git push -u origin “master”
Here when initializing the warehouse for the first time before, in step 6, the warehouse has been bound
, so here no need to bind again
, no need to execute again git remote add origin [email protected]:djcking/myapplication01.git
, push directly to the remote warehouse
.
Note
:
1. The -u parameter
here is generally used during the first push
, which is equivalent to establishing a local branch ( This is the first contact between the master branch)
and the corresponding remote branch (orgin/master) of . Later
push this local branch
to the remote warehouse, without adding the -u parameter
and directly execute git push origin "master"
.
2 If this branch was pulled from git, you can push it after modification without adding the -u parameter. Just execute git push -u origin "master"
directly. Because the first contact
between this local branch and the corresponding remote branch has been established
before, pull to the local workspace
, the contact is also will exist. If you push this local branch
to the remote warehouse later, you do not need to add the -u parameter
and directly execute git push origin "master"
.
3. (Some instructions from "Getting Started with GitHub and Practice")
git push -u origin master or git push -u origin main [Because GitHub’s previous default branch has always been master, but after October 2020, GitHub announced that the default branch for new projects will be master is gradually changed to main. 】
After pushing README.md
to Gitee repository MyApplication
Local warehouse master
and Remote warehouse origin\master
View under command line
:
//View local branches git branch //View all branches (including local and remote) git branch -a
View under Graphical IDE
:
7. Attached are other related blogs written by myself
Submit the project to GitHub using the git command line
Problems with master branch and main branch
5 steps to change GitHub default branch from Master to Main (reprint)
Reference
:
Test SSH connection
Using SSH on the HTTPS port