签名提交、安全与高级技巧
最后一篇——git 安全 + 几个不常用但救命的高级技巧。签名提交是供应链安全的基础(GitHub 的 "Verified" 标);密钥泄漏后的清理流程是每个工程师都该会的应急能力;还有 git archive / git replace / git notes / git worktree --detach 这些 80% 的人不知道但偶尔有奇效的命令。看完这一篇,这个系列就把 git 用透了。
一句话先记住:签名提交 = "这个 commit 真的是我写的、内容没被篡改" ——开源项目和企业代码必装的供应链兜底。密钥泄漏 = 立刻轮换 + 清历史 + 全员 re-clone ——按这三步顺序,不要先想"怎么悄悄删掉"。冷门命令 平时用不到,但有些场景一招就解。
一、为什么要签名提交
git commit 有 author 字段,但任何人都能伪造:
git config --local user.email "linus@kernel.org"
git config --local user.name "Linus Torvalds"
git commit -m "Linus said: ship it!"
# 这个 commit 现在显示作者是 Linusgit 不验证 author——author 字段是不可信的纯文本。
签名提交解决这个问题:用你的私钥对 commit 内容签名,别人用你的公钥验证。
GitHub 显示:
- 签名 + 验证通过:绿色 "Verified" 标
- 签名但 GitHub 没你公钥:黄色 "Unverified"
- 没签名:无标
个人小项目可以不签;开源项目 / 企业仓库 / 发布流水线必签——这是供应链攻击的最后一道防线。
二、GPG 签名
2.1 生成 GPG key
gpg --full-generate-key
# 选 RSA / 4096
# 设过期(推荐 2 年)
# 填名字 + 邮箱(必须和 git user.email 一致)
# 设 passphrase
gpg --list-secret-keys --keyid-format=long
# /Users/qiji/.gnupg/secring.gpg
# sec rsa4096/3AA5C34371567BD2 2026-05-08 [SC]
# 3AA5C34371567BD2...
# uid Qiji Xin <qiji@example.com>3AA5C34371567BD2 是你的 key ID。
2.2 配 git 用 GPG 签
git config --global user.signingkey 3AA5C34371567BD2
git config --global commit.gpgsign true # 默认所有 commit 都签
git config --global tag.gpgsign true # 默认所有 tag 都签之后 git commit 自动签名。
2.3 上传公钥到 GitHub
gpg --armor --export 3AA5C34371567BD2
# 复制输出GitHub Settings → SSH and GPG keys → New GPG key,粘贴。
2.4 验证签名
git log --show-signature -1
# commit a3f5b2c4...
# gpg: Signature made Tue May 5 14:23:45 2026 ...
# gpg: Good signature from "Qiji Xin <qiji@example.com>"三、SSH 签名(2.34+ 推荐)
git 2.34 加入 SSH 签名——复用你已有的 SSH key,不用再搞 GPG。
# 设用 SSH 签
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
# 自动签 commit
git config --global commit.gpgsign trueGitHub 也支持 SSH 签名 key——Settings → SSH and GPG keys → New SSH key,Type 选 Signing Key(不是 Authentication Key)。
好处:
- 不用装 GPG
- key 复用 SSH(你已经在用)
- 配置简单
2026 新项目推荐 SSH 签名。比 GPG 简单一个量级。GPG 留给"必须 GPG"的传统项目(Linux 内核 / Debian 等)。
四、密钥泄漏的应急流程
不管多小心,密钥泄漏发生时的流程是固定的。按顺序做:
4.1 立刻轮换密钥
这是第一步——不要先想"删 commit"。
- 假设密钥已被爬走(GitHub 公开 repo 的
.env文件被攻击者监控,几分钟就被爬) - 立刻去对应平台改密钥(AWS / Stripe / OpenAI 等)
- 改完确认旧密钥失效(用旧 key 调 API 应该报 401)
4.2 通知
- 通知团队
- 通知安全团队(如果有)
- 如果是公开服务的密钥,评估有没有损失(看日志)
4.3 清历史
清密钥(详见 21 篇):
git filter-repo --path .env --invert-paths
git push --force --all
git push --force --tags所有协作者必须 re-clone ——他们本地还有泄漏的 commit。
4.4 防再次发生
- pre-commit hook 加 gitleaks(详见 17 篇)
.gitignore加.env- 用密钥管理服务(AWS Secrets Manager / Vault / Doppler)
一条铁律:密钥进了 git 历史 = 已经泄漏。即使你 30 秒后 force push 删掉,GitHub 已经被监控你的人爬走了。立刻轮换是唯一止损。
五、保护分支与签名要求
GitHub Branch protection 高级选项:
- Require signed commits —— 未签名的 commit 不让进 main
- Require linear history —— 不让 merge commit
- Require status checks —— CI 必须过
配上这些 + 全员配签名 = 强供应链。
六、git archive:导出某 commit 的代码
git archive --format=tar.gz HEAD > snapshot.tar.gz
git archive --format=zip v1.2.0 > v1.2.0.zip
git archive HEAD path/to/dir/ -o subset.tar.gz特点:
- 不带
.git目录(纯代码) - 比
git clone+ 删 .git 快得多 - 适合做源码分发包
给客户打包源码时别用
cp -r+ 手动删 .git——git archive一行,干净彻底。
七、git replace:不改 hash 替换 commit
特殊场景:你想"虚拟改"某个老 commit(比如 fix 一个老 commit message),但不想改 hash。
git replace --edit a3f5b2c # 编辑器改这个 commit 的内容
# 或:
git replace a3f5b2c d8e9f0a # 把 a3f5b2c 替换成 d8e9f0agit log / git show 看到的是替换后的内容,但底层 hash 不变。
推送替换映射:
git push origin 'refs/replace/*'极其罕见——主要给历史考古 / 数据迁移用。日常用不到。
八、git notes:给 commit 加附加备注
不改 commit 本身,加一段"备注":
git notes add -m "deployed to prod on 2026-05-08"
git notes show # 显示当前 commit 的 notes
git log --show-notes # log 时显示 notes典型场景:
- 标记"这个 commit 部署到了哪里 / 何时"
- 加 reviewer 评语
- 关联外部 issue 跟踪
git notes 默认不 push ——要显式:
git push origin refs/notes/commits用得不多,但给老 commit 补背景信息比改历史好——hash 不变,信息加进 notes namespace。
九、git rerere:重用解过的冲突
详见 02 / 07 / 08 篇。开了之后:
git config --global rerere.enabled true第一次解某冲突,git 记下;下次同样冲突自动应用。
最大场景:rebase 一长串 commit,同一冲突重复出现 N 次。
# 看 git 记下了哪些
ls .git/rr-cache/十、git bundle:跨网络传仓库
把整个仓库(或部分)打包成单文件,不需要服务器:
git bundle create snapshot.bundle --all # 整个仓库
git bundle create snapshot.bundle main # 只 main 分支
git bundle create incremental.bundle ~/last-bundle.commit..main # 增量接收方:
git clone snapshot.bundle local-repo
# 或对已有仓库:
git fetch snapshot.bundle main:main典型场景:
- 网络隔离环境(气隙网络)
- 给客户传一个仓库的快照(比 archive 强,带历史)
- 备份
十一、git diff 的进阶
11.1 word-diff(按词,不按行)
git diff --word-diff改了一行里的一个词时,默认显示整行换。--word-diff 只高亮改的词——长行更易读。
11.2 跨语言识别函数
git diff --function-context显示改动所在的整个函数——上下文丰富。
11.3 ignore whitespace
git diff -w # 忽略所有空格变动
git diff --ignore-blank-lines格式化造成的 diff 噪声 → 用 -w 看真改动。
11.4 比较任意两文件(不必在 git 里)
git diff --no-index file1.txt file2.txtgit diff 当通用 diff 工具用——比 diff 命令输出好看。
十二、.gitattributes:文件级属性
*.png binary # 当二进制处理(不显示文本 diff)
*.md text eol=lf # 强制 LF 换行
*.sh text eol=lf
*.bat text eol=crlf # bat 文件用 CRLF
*.psd filter=lfs # 走 LFS
src/generated/* linguist-generated # GitHub 不算这些行进语言统计
docs/* linguist-documentationlinguist-generated / linguist-documentation / linguist-vendored 三个标记让 GitHub 的 "Languages" 统计更准——把生成代码 / 文档 / 三方库排除。
十三、几个救命的小技巧
13.1 撤销最近一次危险操作
git reset --hard ORIG_HEAD # ORIG_HEAD 是 git 在 merge / rebase / pull 前自动留的13.2 看每个分支最后改是什么时候 / 谁
git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:relative) %(authorname)'清理半年没动的分支用得着。
13.3 看仓库统计
git log --shortstat --since="1 month ago" | grep -E "files? changed" | awk '{ insert += $4; delete += $6 } END { print "Lines added:", insert; print "Lines removed:", delete }'13.4 找最早 commit
git log --reverse --oneline | head -113.5 把当前工作区 patch 化
git diff > patch.diff # 工作区 vs HEAD
git diff --cached > staged.diff # staging vs HEAD
# 应用:
git apply patch.diff典型用途:在另一台机器上复现本地改动。
13.6 commit 之后想换分支放
# 在错的分支上 commit 了
git log --oneline -3
# 73a4b6c (HEAD -> wrong-branch) feat: ...
# d8e9f0a previous
# 把这 commit 移到正确分支
git switch -c correct-branch # 在当前位置建对的分支
git switch wrong-branch
git reset --hard HEAD^ # wrong-branch 回到之前
git switch correct-branch # 切到对的,刚 commit 在这了十四、git 的"哲学性"几句话
写到这,这个系列结束。几句给后续的话:
- git 不复杂,只是反直觉 —— 看清"DAG + 指针 + 三层模型"之后,所有命令都是这套机制的不同入口
- reflog 是 git 给你的勇气 —— 知道任何操作都能撤销之后,你才敢动 git
- commit 的颗粒度反映工程纪律 —— 一团 wip 的 commit 历史 = 团队没文化
- PR 大小反映你的拆解能力 —— 1000 行 PR = 你没拆好任务
- 工作流匹配产品阶段 —— 没"最好的"工作流,只有"对的"
- 协作 git 的规则比技术多 —— branch protection / signed commits / CODEOWNERS 是真正的执行力
- git 不是用来"记录代码"的,是用来"沟通"的 —— commit message / PR 描述 / 标签 都是给人看的
十五、踩坑提醒(最后一组)
- 不签名 commit——开源 / 企业项目里被人冒充
- 密钥泄漏先想"删历史"不先轮换——key 已被爬走,删历史晚了
.gitattributes没 commit——团队 LFS / 换行规则不一致git replace/git notes不 push——只你本地有- 不知道
ORIG_HEAD——merge / rebase 后想撤靠 reflog 翻 git archivevsclone --depth=1分不清——archive 不带 .git,clone 带最近 commit- GPG key 过期不换——签名突然全 invalid
- 签名邮箱和 git config 不一致——签名没用,GitHub 显示 unverified
系列总结(2026/05/08 完结)
22 篇全部完成。再读一遍系列地图:
01-05 基础:DAG / 配置 / 三层模型 / commit / log
06-10 分支与协作:branch / merge / rebase / push / tag
11-15 应急:stash / cherry-pick / 撤销 / reflog / gitignore
16-22 工程化:submodule / hooks / worktree / 工作流 / commit规范 / 大仓库 / 安全学完之后该能做到:
- 不查文档解大多数 git 操作
- 出 git 事故 30 秒救回
- 写规范 commit / PR
- 选对工作流
- 防供应链事故
下一步——把这个系列学到的应用到真实项目里,git 是肌肉记忆,只看不练永远不会成为反射动作。
写于 2026/05/08。 这是 biglearning 仓库的第六个完整系列。下一个系列?未来再说。