我如何从分叉中签出 PR?

How do I checkout a PR from a fork?

我正在使用 github 来托管一些项目,有人分叉了我的 repo 并提交了 pr,我以前从未在 fork 中测试过 pr。我怎样才能检查分支并测试它?我可以将它放入我的回购协议中吗?还是我检查分叉并以这种方式测试分支?

PR 在你的 仓库中。这就是 fork-and-PR 架构的重点。

所以只需 git fetch 并在您的计算机上随心所欲地查看一下。

但是当合并的时候,是的,你将从分叉中拉出分支。幸运的是,您可以让 GitHub 为您做这件事。

有几种不同的方法,但我将讨论在这种情况下我会做什么

git remote add <whatever you want the remote to be called> <link to the fork>

Here is the documentation。这将允许您从分叉中添加和签出远程分支。在您描述的工作流程中审查 PR 时,我通常会做一个 git clean -dfx(警告:这是一个 非常 的密集清理,可以摆脱您的未分级工作),git remote add <whatever you want the remote to be called> <link to the fork>,以及 git checkout <branch name>

如果它已经在你的 repo 中,你可以使用 git branch -a 看到它,并像其他方式一样简单地检查它。

基本上,“上游”GitHub 存储库提供对分支的 PR 分支的直接访问(只读),以便您可以获取给定的 PR 并测试相应的代码 after specifying the appropriate ref, pull/ID/head

为此,我经常使用以下别名(可用于 初始提取 ,然后 后续提取 如果PR 已更新——因此覆盖 pr/ID 本地分支):

$ git pr
Usage: git pr <id> [<remote>]  # assuming <remote>[=origin] is on GitHub

$ git pr 101  # fetch PR #101 from origin -> branch pr/101

$ git pr 101 upstream # fetch PR #101 from upstream -> branch pr/101

Disclaimer: this definition is not the very last version of the command, see Further automation below.

这是单行定义,然后是扩展形式:

$ 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/""/head:pr/"" && git checkout pr/""; fi; }; f'

# 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/""/head:pr/"" &&
  git checkout pr/"";
  fi; }; f'

但是请注意,您不能直接推送到这些特殊分支。

如果您想作为维护者直接修改 PR (commit+force-push),您应该按照@yes-siz 的建议将分支添加为另一个 remote(已提供the PR author authorized such access when opening their PR).

进一步自动化

可以注意到上面的 git pr 别名创建了一个 只读 分支,因为能够强制推送到与PR 将需要推送到源分支存储库中的特定分支(假设其所有者授权此访问,如前所述)。

幸运的是,GitHub's REST API allows one to easily retrieve this information (the GitHub source repository as well as the source ref), so that one can automate this by relying on a simple curl call and a few standard Git commands including git branch --set-upstream-to

所有东西放在一起,我们可以获得一些方便的Bash命令:

$ git prw

Facility to fetch a read/write branch from a GitHub repo Pull Request.

Usage:
  git prw <ID> [<REMOTE>] [-f]

Example:
  git prw 390 upstream

Summary:
  If the REMOTE argument is omitted, it defaults to "origin".
  REMOTE must point to a GitHub repository.

  This command checkouts the branch named "pr/ID" (if it doesn't exist
  yet, it fetches the source branch for the PR #ID in the REMOTE repo)
  and sets its upstream branch so that one can force-push to the fork
  (using an SSH URL); it reuses (if applicable) an existing remote
  matching that URL, or creates a remote named REMOTE-fork-for-pr-ID.

  Flag -f overwrites the local branch pr/ID even if it already exists.
  In general, it is a good idea to pass flag -f, unless we already ran
  "git pr ID REMOTE" and did commits in the local branch pr/ID.

  It requires curl <https://curl.se/> and (optionally) jq.

See also:
  - 
  - https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally
  - https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork

我发布了这个 git-prw command in my git-scripts GitHub repository (given it is not a one-liner script any longer :) and this repository also contains an up-to-date version of the git-pr 命令。

由于您使用的是 GitHub,我首选的方法是使用官方 GitHub CLI。

https://github.com/cli/cli

使用GitHub CLI,您可以执行

gh pr checkout 101

结帐 pr #101

在 CLI 中还有很多其他关于拉取请求的有用命令。

干净的具体答案CI 机器最小检查可以用于 Github PRs 与来自 forks 的源分支。

首先,为了清楚起见,一些环境变量:

# This is likely provided by the CI provider. Just the normal clone URL.
GIT_HTTP_URL = "git@github.com:organization/repo.git"
# This is the pull request number. Example: https://github.com/organization/repo/pull/14726
PULL_REQUEST_ID = 14726

命令 运行 稀疏检出 PR:

$ git init
$ git remote add origin $GIT_HTTP_URL
$ git fetch --jobs=10 --no-tags --depth=1 origin "refs/pull/$PULL_REQUEST_ID/head:pull/$PULL_REQUEST_ID"
$ git checkout "pull/$PULL_REQUEST_ID"

这需要 PR 校验时间(对于我相当大的示例回购)从 ~1 分钟到 ~10 秒。

我的方案(如果与您的不相同,则接近)是维护一个 Github 其他人已经分叉的回购,并提交 PR。这是我审查分叉 PR 的过程:

git remote add <forked-repo-org-or-name> <forked-repo-github-url>

检查遥控器是否已添加:

git remote -v

获取最新更新的分支仓库:

git fetch <forked-repo-org-or-name>

检查分叉的 PR 分支:

git checkout <name-of-pr-branch>

现在可以在本地测试分支,探索,决定是否合并,评论等

您不需要访问叉子。只需使用 PR ref 从主仓库中获取它。如果这是 PR 3,则像这样获取 pull/3/head

git fetch origin pull/3/head:my_new_branch
git checkout my_new_branch