Git 如何从叉子签出PR?
我正在使用github主持一些项目,有人将我的回购协议分叉并提交了一份pr,我以前从未在分叉中测试过pr。如何签出分支并对其进行测试?我能告诉你怎么把它纳入我的回购协议吗?或者我要检查分叉并用这种方式测试分支吗?PR在您的回购协议中。这就是fork和PR架构的全部要点 所以只要Git 如何从叉子签出PR?,git,github,git-fork,Git,Github,Git Fork,我正在使用github主持一些项目,有人将我的回购协议分叉并提交了一份pr,我以前从未在分叉中测试过pr。如何签出分支并对其进行测试?我能告诉你怎么把它纳入我的回购协议吗?或者我要检查分叉并用这种方式测试分支吗?PR在您的回购协议中。这就是fork和PR架构的全部要点 所以只要git fetch,然后在您的机器上随意查看一下 但到了合并的时候,是的,你会从分叉处拔出分支。幸运的是,您可以让GitHub为您做这件事。PR在您的回购协议中。这就是fork和PR架构的全部要点 所以只要git fetc
git fetch
,然后在您的机器上随意查看一下
但到了合并的时候,是的,你会从分叉处拔出分支。幸运的是,您可以让GitHub为您做这件事。PR在您的回购协议中。这就是fork和PR架构的全部要点
所以只要git fetch
,然后在您的机器上随意查看一下
但到了合并的时候,是的,你会从分叉处拔出分支。幸运的是,您可以让GitHub为您这样做。有两种不同的方法,但我将讨论在这种情况下我会做什么
git远程添加
。这将允许您从fork添加和签出远程分支。在您描述的工作流中查看PRs时,我通常执行git clean-dfx
(警告:这是一次非常密集的清理,可以清除您的未过期工作),git remote add
,以及git checkout
如果它已经存在于你的回购协议中,你可以在git branch-a中看到它,然后简单地检查它,就像你可能不这样做一样 有两种不同的方法,但我会讨论在这种情况下我会怎么做
git远程添加
。这将允许您从fork添加和签出远程分支。在您描述的工作流中查看PRs时,我通常执行git clean-dfx
(警告:这是一次非常密集的清理,可以清除您的未过期工作),git remote add
,以及git checkout
如果它已经存在于你的回购协议中,你可以在git branch-a中看到它,然后简单地检查它,就像你可能不这样做一样 基本上,“上游”GitHub存储库提供了对forks的PR分支的直接访问(只读),因此您可以获取给定的PR并测试相应的代码
为此,我经常使用以下别名(可用于初始提取,如果PR已更新,则可用于后续提取−从而覆盖pr/ID
本地分支):
但是请注意,您不能直接推送到这些特殊分支
如果您想作为维护人员直接修改PR(提交+强制推送),那么您应该按照@yes siz(已提供)的建议,将fork添加为另一个remote
进一步自动化
可以注意到,上面的git pr
别名创建了一个只读分支,因为如果能够强制推送到与pr关联的分支,则需要推送到源fork存储库中的特定分支(假设其所有者授权了此访问,如前所述)
幸运的是,它允许您轻松地检索这些信息(GitHub源存储库以及source ref),这样您就可以依靠一个简单的调用和一些标准Git命令(包括)实现自动化
综合所有因素,我们可以获得一些方便的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://stackoverflow.com/a/62432946/9164010
- 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
从GitHub repo请求获取读/写分支的工具。
用法:
吉特prw[][-f]
例子:
吉特prw 390上游
总结:
如果省略远程参数,则默认为“origin”。
远程必须指向GitHub存储库。
此命令将签出名为“pr/ID”的分支(如果它不存在)
然而,它获取远程repo中PR#ID的源分支)
并设置其上游分支,以便可以强制推到分叉处
(使用SSHURL);它重用(如果适用)现有的远程服务器
匹配该URL,或创建名为remote-fork-for-pr-ID的远程文件。
标志-f覆盖本地分支pr/ID,即使它已经存在。
一般来说,通过flag-f是一个好主意,除非我们已经运行了
“git pr ID REMOTE”并在本地分支pr/ID中进行了提交。
它需要curl和(可选)jq。
另见:
- https://stackoverflow.com/a/62432946/9164010
- 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
我发布了这个(因为它不再是一个单行脚本:),并且这个存储库还包含了命令的最新版本。基本上,“上游”GitHub存储库提供了对forks的PR分支的直接访问(只读),以便您可以获取给定的PR并测试相应的代码
为此,我经常使用以下别名(可用于初始提取,如果PR已更新,则可用于后续提取−从而覆盖pr/ID
本地分支):
但是请注意,您不能直接推送到这些特殊分支
如果您想作为维护人员直接修改PR(提交+强制推送),那么您应该按照@yes siz(已提供)的建议,将fork添加为另一个remote
进一步自动化
可以注意到,上面的git pr
别名创建了一个只读分支,因为如果能够强制推送到与pr关联的分支,则需要推送到源fork存储库中的特定分支(假设其所有者授权了此访问,如前所述)
幸运的是,它允许您轻松地检索这些信息(GitHub源存储库以及source ref),这样您就可以依靠一个简单的调用和一些标准Git命令(包括)实现自动化
把所有的东西放在一起,我们可以得到一些h
$ 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'
# 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'
$ 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://stackoverflow.com/a/62432946/9164010
- 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