来玩 GitHub 啊,SSH 连接方式

Windows 11

git version 2.32.0.windows.2

GitHub 20240520

--

 

今天找回了自己的 GitHub 账号密码,继续玩吧,再次加入 蓝星的开源软件基地。

使用邮箱注册的,找回密码也很方便。

 

本文简要展示 按照官方文档的介绍 使用 SSH 连接 GitHub 的过程。

简述为:

  1. 创建SSH密钥对
  2. 公钥注册到GitHub,2次
  3. 测试SSH密钥对
  4. 创建仓库
  5. 拉取仓库
  6. 提交代码

 

主要文档

1、Connecting to GitHub with SSH

https://docs.github.com/en/authentication/connecting-to-github-with-ssh

  1. About SSH
  2. Using SSH agent forwarding
  3. Managing deploy keys
  4. Checking for existing SSH keys
  5. Generating a new SSH key and adding it to the ssh-agent
  6. Adding a new SSH key to your GitHub account
  7. Testing your SSH connection
  8. Working with SSH key passphrases

 

2、Getting started with Git

https://docs.github.com/en/get-started/getting-started-with-git

  1. Set up Git
  2. Setting your username in Git
  3. Caching your GitHub credentials in Git
  4. Why is Git always asking for my password?
  5. Updating credentials from the macOS Keychain
  6. Git workflows
  7. About remote repositories
  8. Managing remote repositories
  9. Associating text editors with Git
  10. Configuring Git to handle line endings
  11. Ignoring files
  12. Git cheatsheet

 

使用 SSH 连接 GitHub

文档1 下面的 “E. Generating a new SSH key and adding it to the ssh-agent”。

生成密钥对文件命令:公钥、私钥

ssh-keygen -t ed25519 -C "your_email@example.com"

注,如果密钥对文件已存在,则替换;也可以在 提示“Enter file in which to save the key (/c/Users/YOU/.ssh/id_ALGORITHM):[Press enter]”时输入不同的。

注,在提示,“Enter passphrase (empty for no passphrase): [Type a passphrase]”时,直接按下 回车键, 意味着没有 passphrase,存在安全风险

上面命令执行后,在 HOME 目录的 .ssh 目录下生成了 下面两个文件

  • id_ed25519 私钥
  • id_ed25519.pub 公钥

其中,公钥 需要添加到 GitHub 网站,见文档1 的 “F. Adding a new SSH key to your GitHub account”。

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

说明,SSH key 有两个用途,认证 和 上传代码等,每次添加,都只能选择一个用途,因此,需要添加两次——不过,两次可以是同一个公钥。

Authentication keys 和 Signing keys

验证是否配置成功

见文档1  的 “G. Testing your SSH connection”。

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection

执行命令:

ssh -T git@github.com

说明,第一次会出现很长一串信息,输入 yes 即可。在执行一次,信息就简单了。两次执行的输出信息末尾都有:

Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

--xxx 是 用户名。

 

关于 ssh-agent

在 设置了 密钥对的  passphrase 时,可以使用 ssh-agent 进行管理。

避免每次操作都要输入 passphrase。

安全相关,重要。

 

 

使用代码仓库

见文档2 中的内容。

安装 git软件,略。

 

git配置

见文档2 的 “B. Setting your username in Git”:

https://docs.github.com/en/get-started/getting-started-with-git/setting-your-username-in-git

命令:

git config --global user.name "Mona Lisa"
$ git config --global user.name
--全局
git config user.name "Mona Lisa"
$ git config user.name
--单个库 进入 库目录

 

见文档“Setting your commit email address”:

命令:

git config --global user.email "YOUR_EMAIL"

$ git config --global user.email

--全局

git config user.email "YOUR_EMAIL"

$ git config user.email

--单个库 进入 库目录

注意,其中有提到“a GitHub-provided noreply email address”,GitHub提供的无回复邮件地址。可以配置到Git仓库中。

这个邮件地址的格式也在文档中有介绍,新的是“ID+USERNAME@users.noreply.github.com”,如下:

注,ID找了半天没找到,最后拉取仓库后,使用 git log -3 命令看到了,而且这里的 加号 是真的存在的。

 

创建仓库

在 GitHub 官网,登录后创建,很方便。

 

拉取仓库到本地:SSH

选择 仓库的 Local - SSH 地址:

执行 git clone 拉取到本地 即可。

git clone git@github.com:xxx/hello-world.git

--

拉取后,配置 git 的 user.name、user.email

ben发布于博客园

提交代码到GitHub

命令提交 或 IDE提交,皆可。

不再赘述。

 

END

 

本文链接:

https://www.cnblogs.com/luo630/p/18202797

 

ben发布于博客园

ben发布于博客园

 

posted @ 2024-05-20 22:22  快乐的凡人721  阅读(2)  评论(0编辑  收藏  举报