Detailed process of generating and configuring SSH public and private keys simultaneously for GitHub&Gitee&Gitlab&JihuLab

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 ( to use , for example ) when communicating between your computer and platforms such as GitHub , but it is not as fast as (for example ). Today let’s learn how to use SSH public keys to communicate between computers and platforms such as GitHub .Git的RemoteSSH地址[email protected]:king/example.gitHTTPShttps://gitee.com/king/example.git使用安全连接配置的详细过程

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   "你的名字"
$ git config --global user.email  "你的邮箱"

You can use the following command to clear global settings [Recommended]

$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的邮箱"

Use the git config --global --list command to check whether you have successfully cleared the global settings : If user and email do not appear , it means that the global settings have been 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 and private keys will be generated at the same time . The one 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 local machine ~/.ssh directory [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&Gitee&Gitlab&JihuLab). 3. When

submitting code, confirm the user identity and submission platform by verifying the corresponding SSH public and private keys.

Generate SSH public and private keys

【此处邮箱123456@qq.com不是默认,要修改成开发者个人配置的】
1、生成一个Gitee用的SSH-Key。
ssh-keygen -t rsa -C123456@qq.com-f ~/.ssh/gitee_id_rsa

2、生成一个GitHub用的SSH-Key。
ssh-keygen -t rsa -C123456@qq.com-f ~/.ssh/github_id_rsa

3、生成一个GitLab用的SSH-Key。
ssh-keygen -t rsa -C '123456@qq.com' -f ~/.ssh/gitlab_en_id_rsa

4、生成一个极狐(JihuLab)用的SSH-Key
ssh-keygen -t rsa -C '123456@qq.com' -f ~/.ssh/gitlab_id_rsa


注意输入生成SSH-key指令后,需要按回车3次,表示empty,即“空”,“空”代表没有密码。

The rsa in the ssh-keygen command is an algorithm for generating keys . You can also replace other algorithms to generate ssh keys according to your needs .
For examplessh-keygen -t ed25519 -C "[email protected]" 这里使用的就是ed25519算法, 之后到对应平台重新添加公钥即可

1.3 Screenshot of the process of generating GitLab's SSH public and private keys (for other platforms, just replace the ssh-keygen command)

Insert image description here

1.4 Contents of private key gitee_id_rsa and public key gitee_id_rsa.pub

Private key gitee_id_rsaPublic
Insert image description here
key gitee_id_rsa.pub
Insert image description here

1.5 The SSH public key and private key finally generated by GitHub&Gitee&Gitlab&JihuLab

Insert image description here
In the same way, GitHub, Gitee, and JihuLab also repeat the same process above , and just replace the ssh-keygen command of the corresponding platform , which will not be described again here.

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& Gitee& Gitlab& is 极狐(JihuLab)configured at the same time SSH公私钥, it is different from configuring only one platform separately. In order to do this 避免混淆不同平台的SSH公私钥, you need to create a new file in ~/.sshthe directory and add the following content ( fill in and , specify )【即C:\Users\用户名\.ssh目录】configHostHostNamegit服务器的域名IdentityFile私钥的路径

# github
# ssh -T git@github.com
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa





#2023-3-3 00:57:06
# gitee
# ssh -T git@gitee.com
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa



#极狐第一种
#2023-4-5 22:37:31  jihulab
#ssh -T git@gitlab.cn
Host gitlab.cn
Hostname altssh.jihulab.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa
   
#极狐第二种
#2023-4-5 22:37:31  jihulab
#ssh -T git@jihulab.com
Host jihulab.com
Hostname altssh.jihulab.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa
  




  
#2023-4-6 01:00:05
#ssh -T git@gitlab.com
#gitlab 
Host     gitlab.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 to 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 keys to Gitee and GitHub. (You will be asked to enter a password for authentication when adding)

3.1 Configure the public key gitee_id_rsa.pub in Gitee

//查看生成的Gitee公钥gitee_id_rsa.pub,复制Gitee公钥gitee_id_rsa.pub
//或者直接去C:\Users\用户名\.ssh目录下直接用文本编辑器打开Gitee公钥gitee_id_rsa.pub,复制Gitee公钥gitee_id_rsa.pub的内容
cat  ~/.ssh/gitee_id_rsa.pub

Copy the Gitee public key gitee_id_rsa.pub:
Insert image description here
click Account Settings
Insert image description here
Insert image description here

Add the title and Gitee public key gitee_id_rsa.pub content and click OK , then enter the password for authentication :
Insert image description here
Insert image description here
Added successfully :
Insert image description here

3.2 Configure the public key github_id_rsa.pub in GitHub

//查看生成的GitHub公钥github_id_rsa.pub,复制GitHub公钥github_id_rsa.pub
cat  ~/.ssh/github_id_rsa.pub

Click Settings
Insert image description here
, click New SSH Key
Insert image description here
, enter the title & copy and paste the contents of the public key github_id_rsa.pub & click Add SSH Key
Insert image description here

3.3 Configure the public key gitlab_en_id_rsa.pub in Gitlab

//查看生成的Gitlab公钥gitlab_en_id_rsa.pub,复制Gitlab公钥gitlab_en_id_rsa.pub
cat  ~/.ssh/gitlab_en_id_rsa.pub

Click on Preferences
Insert image description here
Insert image description here

Click SSH Keys,
Insert image description here
click Add new key,
Insert image description here
fill in the relevant information of the Gitlab public key gitlab_en_id_rsa.pub, and click Add key :
Insert image description here

3.4 Configure the public key gitlab_id_rsa.pub in JihuLab

//查看生成的极狐(JihuLab)公钥gitlab_id_rsa.pub,复制极狐(JihuLab)公钥gitlab_id_rsa.pub
cat  ~/.ssh/gitlab_id_rsa.pub

Click on Preferences,
Insert image description here
click on SSH Key
Insert image description here
, click on Add New Key,
Insert image description here
fill in the key - title - usage type - expiration time and other information - click on Add Key

Insert image description here

4. Check whether the configuration is successful

4.1 Detection command

When using it for the first time, you will see Are you sure you want to continue connecting (yes/no/[fingerprint]? Enter yes . [Indicates confirmation to add the host to the trusted list]

//检测GitHub
ssh -T git@github.com

//检测gitee
ssh -T git@gitee.com

//检测极狐(JihuLab)【中国代理商运营的Gitlab】
ssh -T git@gitlab.cn
或
ssh -T git@jihulab.com

//检测Gitlab
ssh -T git@gitlab.com

4.2 Screenshot of successful execution of detection command

Insert image description here
Insert image description here

5. Git SSH and HTTPS command line introductory tutorial

Note : SSH [email protected]:djcking/myapplication01.gitand HTTPS https://gitee.com/djcking/myapplication01.gitcan be directly copied and pasted from the warehouse.以哪种方式配置,就复制使用哪一种。

5.1 SSH (used in conjunction with the tutorial above)

Git global settings

git config --global user.name "GiHub用户名"
git config --global user.email "GitHub邮箱"
a:还没有git仓库,创建 git 仓库(此处以myapplication01仓库为例):
//1-创建myapplication01仓库
mkdir myapplication01

//2-进入myapplication01
cd myapplication01

//3-初始化仓库
git init 

//3-创建README.md文件
touch README.md

//4-add README.md
git add README.md

//5-commit
git commit -m "first commit"

//6-绑定远端仓库
git remote add origin [email protected]:djcking/myapplication01.git

//7-推送到远端origin的master分支
git push -u origin "master"
或者
git push -u origin main 
【因为GitHub之前的默认分支一直就是master,但2020年10月后GitHub宣布:新建项目的默认分支将从
master逐步改为main。】

b:已有git仓库:

//1-进入已经存在的git仓库(此处以myapplication01仓库为例)
//cd existing_git_repo
cd myapplication01

//2-绑定远端仓库
git remote add origin [email protected]:djcking/myapplication01.git

3-推送到远端origin的master分支
git push -u origin "master"
或者
git push -u origin main 
【因为GitHub之前的默认分支一直就是master,但2020年10月后GitHub宣布:新建项目的默认分支将从
master逐步改为main。】

更简单的使用方式: Direct 建远端仓库后, initialization (git init) and binding warehouse (git remote clone到本地add 无需origin) can be used directly.

5.2 HTTPS (Git needs to be configured in HTTPS mode)

Git global settings

git config --global user.name "GiHub用户名"
git config --global user.email "GitHub邮箱"
a:还没有git仓库,创建 git 仓库(此处以myapplication01仓库为例):
//1-创建myapplication01仓库
mkdir myapplication01

//2-进入myapplication01
cd myapplication01

//3-初始化仓库
git init 

//3-创建README.md文件
touch README.md

//4-add README.md
git add README.md

//5-commit
git commit -m "first commit"

//6-绑定远端仓库
git remote add origin https://gitee.com/djcking/myapplication01.git

//7-推送到远端origin的master分支
git push -u origin "master"
或者
git push -u origin main 
【因为GitHub之前的默认分支一直就是master,但2020年10月后GitHub宣布:新建项目的默认分支将从
master逐步改为main。】

b:已有git仓库:
//1-进入已经存在的git仓库(此处以myapplication01仓库为例)
//cd existing_git_repo
cd myapplication01

//2-绑定远端仓库
git remote add origin https://gitee.com/djcking/myapplication01.git

//3-推送到远端origin的master分支
git push -u origin "master"
或者
git push -u origin main 
【因为GitHub之前的默认分支一直就是master,但2020年10月后GitHub宣布:新建项目的默认分支将从
master逐步改为main。】

更简单的使用方式: Direct 建远端仓库后, initialization (git init) and binding warehouse (git remote clone到本地add 无需origin) can be used directly.

6. Add file README.md using Git command line (SSH)

未推送README.md的Gitee仓库MyApplication
Insert image description here
Insert image description here
往Gitee仓库MyApplicationScreenshot of pushed README.md后command
Insert image description here
推送README.md后Gitee仓库MyApplication
Insert image description here

6.1 Before creating README.md

Insert image description here

6.2 touch README.md

Insert image description here

6.3 git add README.md

git add 命令可将该文件的修改添加到暂存区。
通过运行 git add 命令,你可以告诉 Git 哪些文件的修改应该包含在下一次提交(commit)中。
将文件从workdir(工作目录)中添加到缓存区index(即暂存区stage)(出现红色感叹号)

//*表示所有文件 
git add *

//.表示当前目录下的所有文件 
git add .

//也可以直接添加目录
 git add 目录名

//添加一个或多个文件 
git add 文件1名 文件2名

//强制提交某个文件(不管.gitignore是否包含了这个文件)
//.gitignore文件是用来配置  忽略不需要上传的文件  的文件
git add -f 文件名

Insert image description here

6.4 git commit -m “add README.md”

git commit - m代码提交信息

代码提交信息Equivalent to 代码中的注释.

After execution, it will be 根据摘要算法(SHA1)生成一个不重复的40位提交ID字符串, by default it will only be 显示7位, 唯一识别你的每一次提交, here it is 731adec.
Insert image description here

6.5 git push -u origin “master”

Here 之前第一次初始化仓库时,第6步,已经绑定过仓库, so here 无需再绑定, no need to execute anymoregit remote add origin [email protected]:djcking/myapplication01.git , 直接push到远端仓库.

注意:
1. Here -u 参数, it is generally 第一次 pushused when you are with, which is equivalent to establishing the first contact 某个本地分支(这里是master分支)with . 对应的远端分支(orgin/master)Then 再push这个本地分支go to the remote warehouse and 无需再加-u参数execute it directly git push origin "master".

2. If this branch is pulled down by git, you can push it after modification without adding the -u parameter and execute it directly , git push -u origin "master"because the previous connection 已经建立过between the local branch and the corresponding remote branch will also exist. Go to the remote warehouse and execute it directly .第一次联系pull到本地workspace再push这个本地分支无需再加-u参数git push origin "master"

3. (Some instructions from "Getting Started with GitHub and Practice")
Insert image description here

git push -u origin master
或者
git push -u origin main 
【因为GitHub之前的默认分支一直就是master,但2020年10月后GitHub宣布:新建项目的默认分支将从
master逐步改为main。】

Insert image description here
推送README.md后of Gitee仓库MyApplication
Insert image description here
本地仓库masterand远端仓库origin\master

命令行View below:

//查看本地分支
git branch 

//查看所有分支(包含本地和远端)
git branch -a

Insert image description here
图形化IDEView below:
Insert image description here

7. Attached are other related blogs written by myself

Problems with using the git command line to submit projects to GitHub
master branch and main branch.
5 steps to change the GitHub default branch from Master to Main (reprinted)

Guess you like

Origin blog.csdn.net/qyfx123456/article/details/132715619