有时候别人给你的仓库提了一个 PR,代码方向是对的,但还差一点小修小补。最直接的做法不是让对方重新来回改,而是 maintainer 直接往这个 PR 的 head branch 上追加 commit。
前提是对方在 PR 里勾选了 Allow edits from maintainers。这个选项的意思是:拥有目标仓库写权限的人,可以把 commit push 到这个 PR 来源 fork 的分支上。
这里记录一下我这次用过的流程。
场景
假设:
- 目标仓库是
S2thend/cursor-history - PR 编号是
30 - PR 来源 fork 是
caseyjhol/cursor-history - PR 来源分支是
feature/fix-migration-session-visibility - PR 作者已经勾选了
Allow edits from maintainers
GitHub PR 页面顶部一般会显示类似这样的关系:
caseyjhol wants to merge ... into S2thend:main from caseyjhol:feature/fix-migration-session-visibility
这里最重要的是后半段:
from caseyjhol:feature/fix-migration-session-visibility
之后 push 的目标就是这个 fork 和这个分支。
最短命令流
先把 PR 的内容 fetch 到一个本地临时分支:
git fetch https://github.com/S2thend/cursor-history.git pull/30/head:pr-30-review
git switch pr-30-review
然后正常修改代码、测试、提交:
git status
git add path/to/changed-file
git commit -m "Fix migration session visibility"
最后把本地 HEAD 推回 PR 的来源 fork 分支:
git push git@github.com:caseyjhol/cursor-history.git HEAD:feature/fix-migration-session-visibility
这条 push 命令是关键。不是 push 到 pull/30/head,而是 push 到 PR 作者 fork 里的真实分支。
为什么 fetch 和 push 的地址不一样
这一步最容易搞混。
pull/30/head 是 GitHub 在目标仓库里暴露出来的 PR ref,适合用来把某个 PR checkout 到本地 review:
git fetch https://github.com/S2thend/cursor-history.git pull/30/head:pr-30-review
但它不是一个你应该直接 push 的普通分支。真正承载 PR 的分支,是 contributor fork 里的 head branch:
caseyjhol/cursor-history
feature/fix-migration-session-visibility
所以 push 要写成:
git push git@github.com:caseyjhol/cursor-history.git HEAD:feature/fix-migration-session-visibility
HEAD:feature/fix-migration-session-visibility 的意思是:把我当前本地分支的最新 commit,推到远端的 feature/fix-migration-session-visibility 分支。
如果本地分支已经存在
如果你之前已经 fetch 过这个 PR,本地 pr-30-review 已经存在,直接 fetch 可能会报错。可以换一个本地分支名:
git fetch https://github.com/S2thend/cursor-history.git pull/30/head:pr-30-review-2
git switch pr-30-review-2
或者确认旧分支不需要后再删除重建:
git branch -D pr-30-review
git fetch https://github.com/S2thend/cursor-history.git pull/30/head:pr-30-review
git switch pr-30-review
更可读的 remote 写法
如果这类操作会重复做,可以把 contributor fork 加成一个临时 remote:
git remote add pr-author git@github.com:caseyjhol/cursor-history.git
git push pr-author HEAD:feature/fix-migration-session-visibility
做完之后也可以删掉:
git remote remove pr-author
我个人在一次性修 PR 时更喜欢直接写完整 push URL,因为它不污染本地 remote 列表,也不容易忘记这个 remote 是临时的。
常见错误
Permission denied
如果 push 时看到类似:
ERROR: Permission to caseyjhol/cursor-history.git denied
通常有几个原因:
- PR 作者没有勾选
Allow edits from maintainers - 你不是目标仓库的 maintainer,或者没有写权限
- 你 push 到了错误的 fork
- 你的 SSH key 没有正确关联到当前 GitHub 账号
先回 PR 页面确认 head branch 到底是哪一个 fork 和哪一个分支,再确认对方是否允许 maintainer edit。
non-fast-forward
如果 contributor 在你本地修改期间又 push 了新 commit,你再 push 可能会遇到 non-fast-forward。
这时候不要急着 force push。先重新 fetch 最新 PR:
git fetch https://github.com/S2thend/cursor-history.git pull/30/head:pr-30-latest
然后把自己的修改 rebase 或 cherry-pick 到最新分支上,再正常 push。除非你和 PR 作者明确沟通过,否则不要对别人的 PR 分支做 force push。
PR 页面没有更新
如果 push 成功但 PR 没更新,多半是推错分支了。检查 PR 页面顶部的 from owner:branch,再对照你的命令:
git push git@github.com:<owner>/<repo>.git HEAD:<branch>
owner 和 branch 必须和 PR 页面显示的来源完全一致。
什么时候适合这样做
适合:
- PR 方向正确,只差小修
- CI 失败原因很明确,maintainer 可以快速修
- contributor 不熟悉项目内部约定,需要 maintainer 帮忙补一刀
- review comment 来回沟通成本比直接 commit 高
不适合:
- 修改会改变 PR 的核心设计
- 需要大规模重写 contributor 的代码
- contributor 没有明确授权 maintainer 修改
- 你需要 force push 或重写历史
这种场景下,直接在 PR 里解释建议,让作者自己改会更合适。
记住这两个方向
fetch 的方向:
git fetch <base-repo-url> pull/<pr-number>/head:<local-branch>
push 的方向:
git push <contributor-fork-url> HEAD:<contributor-branch>
完整例子:
git fetch https://github.com/S2thend/cursor-history.git pull/30/head:pr-30-review
git switch pr-30-review
# edit files...
git add .
git commit -m "Fix migration session visibility"
git push git@github.com:caseyjhol/cursor-history.git HEAD:feature/fix-migration-session-visibility
核心原则就一句话:从目标仓库的 PR ref 拉代码,在本地提交,然后推回 contributor fork 的真实 head branch。
References
- GitHub Docs. Allowing changes to a pull request branch created from a fork. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork
- GitHub Docs. Committing changes to a pull request branch created from a fork. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/committing-changes-to-a-pull-request-branch-created-from-a-fork
- GitHub Docs. Checking out pull requests locally. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally