Skip to content

Git 配置 SSH 连接

上级

base
views:
  - type: table
    name: 关联文件表
    filters:
      and:
        - layer_id == this.parent_id  
        - parent_id != null   
        - layer_id != null

工作内容

base
views:
  - type: cards
    name: 视图
    cardSize: 200
    image: note.title
    filters:
      and:
        - parent_id == this.layer_id
        - parent_id != null
    order:
      - title
    sort:
      - property: priority
        direction: ASC
      - property: layer_id
        direction: ASC

一般我们在使用 git 的时候,除了 http 的方式,使用 ssh 的方式可能也占据了大部分时候(尤其是工作的时候)

配置 SSH 连接

回到Git Bash中,配置Github账户信息(用户名​和邮箱​都替换成你自己Github上的):

text
git config --global user.name "`用户名`"
git config --global user.email "邮箱"

在Git Bash中输入:ssh-keygen -t rsa -C "你的Github邮箱"​ 生成ssh

text
ssh-keygen -t rsa -C "邮箱"

然后按Git Bash给出的路径(C:\Users\Lete.ssh)​找到id_rsa.pub​文件 并复制其中的内容

进到新建的Github仓库 点击右上角setting​进入设置找到Deploykeys​选择 Add Deploy keysTitle​随便填写 Key​的内容填刚才id_rsa.pub​文件中复制的内容

‍ 修改本地Git配置,设置使用SSH协议连接GitHub。使用以下命令修改本地Git配置:

git config --global url."git@github.com:".insteadOf "https://github.com/"

这个命令会将Git配置中的 https://github.com/​ 替换为 git@github.com:​,从而使用SSH协议连接GitHub。

配置后再部署提交

常用的一些命令

查看用户名 :git config user.name

查看密码: git config user.password

查看邮箱:git config user.email

查看配置信息: $ git config --list
  • git checkout 切换分支

设置两个账号切换

参考: https://worktile.com/kb/ask/224209.html

在工作电脑上面进行生成个人账号的 ssh 的密钥

ssh-keygen -t rsa -f ~/.ssh/id_rsa_personal -C "personal_email@example.com"

ssh-keygen -t rsa -f ~/.ssh/id_rsa_personal -C "lluo2020@163.com"

-f 参数用于指定生成的 SSH 密钥文件的路径。当你使用这个参数时,你可以指定一个文件名,ssh-keygen 将会在这个文件中生成私钥。如果不使用 -f 参数,默认情况下,ssh-keygen 会在 ~/.ssh/id_rsa 路径下生成密钥对

创建配置文件

touch ~/.ssh/config

看了一下需要在配置文件中进行对应 host 配置,存在风险,暂时不搞这种方式

The authenticity of host github.com can't be established.

工作电脑切换账户后,不能进行和Github授权访问。

错误失败经历 工作电脑如何使用个人邮箱和GitHub建立连接

connect to host github.com port 22

ssh: connect to host github.com port 22:
Connection timed out
fatal: Could not read from remote
repository
Please make sureyou have the correct
accessrights
andtherepositoryexists

“Connection timed out” 是一个网络层的错误。它意味着你的电脑尝试与 github.com的 22 端口建立 SSH 连接,但在规定的时间内(通常是几十秒)没有收到对方的任何响应

一般可能是网络问题

方式一

让 SSH 走 443 端口

# 确保进入用户主目录(MAC电脑暂时没有实际去创建,但应该差不多);而且实际使用的时候,发现当 windows 下重新建立之后,看mac的项目又能重新建立连接了,感觉也可能是github的机制,具体不太清楚。
cd ~
# 创建.ssh目录(如果它不存在的话)
mkdir -p .ssh
# 进入该目录
cd .ssh
# 创建或编辑config文件
notepad config


#看了一下 window 系统是直接到用户目录下的,比如
C:\Users\Administrator\.ssh
#没有的话手动新建一下 config 文件

保存内容

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

测试连接

ssh -T git@github.com

实际操作出现的内容,,无脑点了一下yes,然后看是能用了,后续有问题再跟进一下。

PS E:\obsidian\gitblog\siiyuanBlog\modules\homepage\tempnote> ssh -T git@github.com
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This host key is known by the following other names/addresses:
    C:\Users\Administrator/.ssh/known_hosts:1: github.com
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
Hi Lluo-liang/siiyuanBlog! You've successfully authenticated, but GitHub does not provide shell access.
PS E:\obsidian\gitblog\siiyuanBlog\modules\homepage\tempnote>

Permission denied to deploy key

新建了一个仓库,但是没有推送的权限

确认当前用的是哪个 key

PS D:\Code\tools\feishushare>ssh -T git@github.com
Hi Lluo-liang/siiyuanBlog! You've successfully authenticated, but GitHub does not provide shell access.
PS D:\Code\tools\feishushare>

提示内容:SSH 使用的 key 是绑定在 siiyuanBlog 这个仓库的 deploy key,而不是你 GitHub 个人账户的 key。


上面这种情况的解决方案:

  • 一、单独为每个仓库,单独添加一个 Deploy keys,然后在ssh/config 文件中配置一下对应的地址和 IdentityFile 文件
  • 二、直接生成一个 key, 添加为你个人账户的 SSH key(最干净)
    • 方案一,将之前的siyuanblog 的部署key删除,然后直接将你电脑本地的 is_rsa.pub 文件的内容放到个人账户下
      • 指定后,再查看一下当前能指定的仓库部署有哪几个: ssh -T git@github.com
    • 方案二,保留仓库指定的部署key,新增一个,但是需要修改一下 ssh/config 文件(需要指定一下 IdentityFile 文件)

不要添加重复 key

你当前这台机器用的 SSH key,已经被添加为 siiyuanBlog 仓库的 deploy key,因此不能再以“普通公钥”形式重复添加到别的仓库。

生成一个新的 SSH key,专门给 feishushare 用,但是需要修改一下 ssh/config 文件

-- 单独文件
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_feishushare
-- window示例
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f C:\Users\Administrator\.ssh/id_rsa_feishushare
-- 使用更安全的 ed25519 算法
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/id_ed25519_feishushare

下面这个显示指已经成功用个人账户的 SSH key 登录 GitHub,直接 git push 是能成功的(账户维度而不是仓库维度)

PS D:\Code\tools\feishushare> ssh -T git@github.com
Hi Lluo-liang! You've successfully authenticated, but GitHub does not provide shell access.
PS D:\Code\tools\feishushare>