关于git操作

生成SSH密钥

生成密钥对后再到 GitHub/Gitee 添加公钥;点击下方按钮可跳转 SSH 笔记查看各算法细节。

RSA-4096 Ed25519 ECDSA-521
  1. RSA-4096(兼容性最好,老系统/旧服务首选)
1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. Ed25519(安全性高、密钥短,推荐)
1
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. ECDSA-521(安全与兼容的折中方案)
1
ssh-keygen -t ecdsa -b 521 -C "your_email@example.com"
  1. 多账户场景务必用 -f 指定独立密钥文件名,与下文配置文件中的 IdentityFile 一一对应
1
ssh-keygen -t rsa -b 4096 -C "xxxx@qq.com" -f ~/.ssh/giteeqq_id_rsa
  1. 回车后按提示选择保存路径并设置 passphrase(口令,可留空),生成两个文件:

    私钥:/.ssh/id_rsa(或 id_ed25519),绝不外传
    公钥:
    /.ssh/id_rsa.pub(或 id_ed25519.pub),可放心添加到网站

  2. 查看公钥内容并复制,粘贴到 GitHub 的 Settings -> SSH and GPG keys 或 Gitee 的 设置 -> SSH公钥
1
cat ~/.ssh/id_rsa.pub
  1. passphrase 忘记无法找回,只能重新生成密钥并重新添加公钥。

SSH验证

  1. 验证本地生成的ssh,github或者gitee端密钥是否添加成功,输入如下命令
1
2
ssh -T git@github.com
ssh -T git@gitee.com

首次连接会提示 Are you sure you want to continue connecting (yes/no)?,输入 yes 即可将主机指纹写入 ~/.ssh/known_hosts。
2. 如果添加成功就会显示如下语句

Hi Username! You’ve successfully authenticated, but GitHub does not provide shell access.
Hi Username! You’ve successfully authenticated, but GITEE.COM does not provide shell access.

  1. Username:是你在 Git 端的名称

同一台电脑上多个github或gitee账户

同一台电脑有两个github账号或者两个gitee账号,配置如下都相同

  1. 请参考:gitee文档 配置文件如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# ===== ~/.ssh/config 文件(Windows 实际路径:C:\Users\你的用户名\.ssh\config)=====
# 每个 Host 段对应一个账户;# 开头为注释,缩进仅作可读性用

# gitee xxxxxx@qq.com(直接用真实域名作别名,clone 时保持 git@gitee.com 写法)
Host gitee.com
HostName gitee.com # 真实主机地址,固定不变
User git # SSH 登录用户,GitHub/Gitee 均为 git
IdentityFile ~/.ssh/giteeqq_id_rsa # 该账户使用的私钥(即 ssh-keygen -f 生成时指定的文件名)
IdentitiesOnly yes # 只用上面这把私钥,避免多密钥轮流尝试导致 Too many authentication failures
PreferredAuthentications publickey # 仅允许公钥认证,跳过密码认证

# gitee xxxxxxx@189.cn
Host xxxx.gitee.com #此处xxxx需要修改为你的gitee名称(别名可任意起,如 a.gitee.com,仅本机识别)
HostName gitee.com
User git
IdentityFile ~/.ssh/gitee189_id_rsa
IdentitiesOnly yes
PreferredAuthentications publickey

# github aaaaa@189.cn
Host aaaaa.github.com #此处aaaaa需要修改为你的github名称
HostName github.com
User git
IdentityFile ~/.ssh/github189_id_rsa
IdentitiesOnly yes
PreferredAuthentications publickey

# github xxxx@qq.com(使用默认密钥 id_rsa 时整段保持注释即可,需要区分时再取消注释)
#Host xxxx.github.com #此处xxxx需要修改为你的github名称
#HostName github.com
#User git
#IdentityFile ~/.ssh/id_rsa
#IdentitiesOnly yes
#PreferredAuthentications publickey
  1. 文件中有多个xxx或aaaaa都是需要修改处,如果需要验证添加SSH是否成功,测试代码如下,对应上面配置文件
1
2
3
4
ssh -T git@gitee.com
ssh -T git@xxxx.gitee.com
ssh -T git@aaaaa.github.com
ssh -T git@xxxx.github.com

依此类推有多少个gitee或github就配置多少个,特别注意 xxxx_id_rsa 不能配置为相同文件名。

补充:若某账户密钥被错误尝试,可在对应 Host 段添加 IdentitiesOnly yes,强制只使用 IdentityFile 指定的密钥;克隆/推送时也要使用别名主机,如 git clone git@xxxx.gitee.com:用户名/仓库名.git,不能直接用 gitee.com。


git 问题

  1. GitHub Desktop 使用ssh推送或者下拉时候报错 Authentication failed. Some common reasons include:

解决方法:

可以尝试关闭路径 File->Options->Advanced->SSH的选项->Use system OpenSSH 选项

  1. fatal: unable to access ‘git/‘: Send failure:Connection was reset

解决方法:

为了将来参考,我通过重新打包解决了这个问题:

git repack --max-pack-size=100M -a -d

git指令

git remote 指令

git remote 命令用于管理 Git 仓库中的远程仓库。

git remote 命令提供了一些用于查看、添加、重命名和删除远程仓库的功能。

1
2
3
4
5
6
7
8
9
10
11
12
git remote   列出当前仓库中已配置的远程仓库。
git remote -v 列出当前仓库中已配置的远程仓库,并显示它们的 URL。

添加一个新的远程仓库。指定一个远程仓库的名称(origin)和 URL(https://github.com/user/repo.git),将其添加到当前仓库中。
git remote add origin https://github.com/user/repo.git

git remote rename origin neworigin 将已配置的远程仓库重命名。 origin 重命名 neworigin
git remote remove deleteorigin 从当前仓库中删除指定的远程仓库。删除 `deleteorigin`

修改指定远程仓库的 URL。将 origin 仓库地址 改成 https://github.com/user/new-repo.git
git remote set-url origin https://github.com/user/new-repo.git
git remote show origin 显示指定远程仓库的详细信息,包括 URL 和跟踪分支。

使用方法:

查看当前的远程仓库URL

1
git remote -v

添加远程仓库URL

1
git remote add origin https://xxxxx.git