Arn*_*mer 19 git github git-fork
我正在使用 GitHub 托管一些项目,有人分叉了我的存储库并提交了 PR。我以前从未需要在 fork 中测试 PR。我如何检查分支并测试它?我可以如何将其放入我的仓库中吗?或者我应该检查分叉并以这种方式测试分支吗?
jsc*_*ter 15
您不需要访问叉子。只需使用 PR 参考从主存储库中获取它即可。如果这是 PR 3,则像pull/3/head这样获取:
git fetch origin pull/3/head:my_new_branch
git checkout my_new_branch
Run Code Online (Sandbox Code Playgroud)
Eri*_*kMD 12
基本上,“上游”GitHub 存储库提供对 fork\' PR 分支(只读)的直接访问,以便您可以获取给定的 PR 并在指定适当的 ref 后pull/ID/head测试相应的代码。
为此,我经常使用以下别名(可用于初始 fetch,如果 PR 已更新,则可用于后续的 fetchpr/ID \xe2\x88\x92 ,从而覆盖本地分支):
$ git pr\nUsage: git pr <id> [<remote>] # assuming <remote>[=origin] is on GitHub\n\n$ git pr 101 # fetch PR #101 from origin -> branch pr/101\n\n$ git pr 101 upstream # fetch PR #101 from upstream -> branch pr/101\nRun Code Online (Sandbox Code Playgroud)\n\n\n免责声明:此定义不是该命令的最新版本,请参见
\nFurther automation下文。
这是单行定义,然后是扩展形式:
\n$ git config --global alias.pr \'!f() { if [ $# -lt 1 ]; then echo "Usage: git pr <id> [<remote>] # assuming <remote>[=origin] is on GitHub"; else git checkout -q "$(git rev-parse --verify HEAD)" && git fetch -fv "${2:-origin}" pull/"$1"/head:pr/"$1" && git checkout pr/"$1"; fi; }; f\'\n\n# git config --global alias.pr \'!f() { if [ $# -lt 1 ]; then\n echo "Usage: git pr <id> [<remote>] # assuming <remote>[=origin] is on GitHub";\n else git checkout -q "$(git rev-parse --verify HEAD)" &&\n git fetch -fv "${2:-origin}" pull/"$1"/head:pr/"$1" &&\n git checkout pr/"$1";\n fi; }; f\'\nRun Code Online (Sandbox Code Playgroud)\n但请注意,您不能直接推送到这些特殊分支。
\n如果您想作为维护者直接修改 PR(提交+强制推送),那么您应该remote按照 @yes-siz 的建议将分叉添加为另一个(前提是PR 作者在打开其项目时授权了此类访问权限)公关)。
可以注意到,git pr上面的别名创建了一个只读分支,因为能够强制推送到与 PR 关联的分支将需要推送到源分支存储库中的特定分支(假设其所有者授权此分支)访问,如前所述)。
幸运的是,GitHub 的 REST API允许人们轻松检索此信息(GitHub 源存储库以及源引用),以便人们可以通过简单的调用curl和一些标准 Git 命令(包括git branch --set-upstream-to.
把所有东西放在一起,我们可以获得一些方便的 Bash 命令:
\n$ git prw\n\nFacility to fetch a read/write branch from a GitHub repo Pull Request.\n\nUsage:\n git prw <ID> [<REMOTE>] [-f]\n\nExample:\n git prw 390 upstream\n\nSummary:\n If the REMOTE argument is omitted, it defaults to "origin".\n REMOTE must point to a GitHub repository.\n\n This command checkouts the branch named "pr/ID" (if it doesn\'t exist\n yet, it fetches the source branch for the PR #ID in the REMOTE repo)\n and sets its upstream branch so that one can force-push to the fork\n (using an SSH URL); it reuses (if applicable) an existing remote\n matching that URL, or creates a remote named REMOTE-fork-for-pr-ID.\n\n Flag -f overwrites the local branch pr/ID even if it already exists.\n In general, it is a good idea to pass flag -f, unless we already ran\n "git pr ID REMOTE" and did commits in the local branch pr/ID.\n\n It requires curl <https://curl.se/> and (optionally) jq.\n\nSee also:\n - /sf/answers/4370306251/\n - https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally\n - https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork\nRun Code Online (Sandbox Code Playgroud)\n我在GitHub 存储库中发布了此git-prw命令git-scripts(假设它不再是单行脚本:),并且此存储库还包含该命令的最新版本git-pr。
有几种不同的方法,但我\xe2\x80\x99将回顾在这种情况下我会做什么
\n\ngit remote add <whatever you want the remote to be called> <link to the fork>
这是文档。这将允许您从分支添加和签出远程分支。在审查您描述的工作流程中的 PR 时,我通常会执行以下操作git clean -dfx(警告:这是一次非常密集的清理,会消除您所拥有的未分阶段的工作)、git remote add <whatever you want the remote to be called> <link to the fork>和git checkout <branch name>。
如果它\xe2\x80\x99s已经在你的仓库中,你可以看到它并git branch -a简单地检查它,否则你可能会这样。
| 归档时间: |
|
| 查看次数: |
9344 次 |
| 最近记录: |