如何获取我不拥有的分支的未合并拉取请求?

问题描述 投票:65回答:11

我需要在NServiceBus repo中提取一个特定的pull请求(尚未处理到主流中):

https://github.com/johnsimons/NServiceBus/commit/d8524d53094e8181716e771c1023e968132abc15

这显然不是我的回购,但我需要拉动请求中存在的更改。

做这个的最好方式是什么?

git github git-pull pull-request
11个回答
69
投票

要获取存储库中的内容:

git fetch [email protected]:jboss/jboss-common-beans.git refs/pull/4/head

然后用FETCH_HEAD做任何你想做的事:

git checkout -b new-branch FETCH_HEAD

0
投票

如果您只想将一个未合并的Pull请求从其他repo添加到您自己的,则不需要所有复杂情况(如其他答案中所示)。

相反,只需使用其提交哈希,进入您自己的仓库并提交提交(来自PR源)。

git pull https://bitbucket.org/SomeUser/SomeProjectRepo/commits/c15...db2

这样做,你将只有一堆新的编辑文件,就好像你自己编辑过它们一样。如果你想用一些标签/标签提交这些,那么由你决定。

如果您想将所有新闻推送到您自己的GitHub仓库,请一如既往:

git commit -m "Added something by Anonymous"
git push -u origin master

0
投票

Github有明确的文档将拉取请求合并到本地仓库中:

https://help.github.com/en/articles/checking-out-pull-requests-locally

归结为知道GitHub中的pull请求只是基本仓库中的分支,其中分支名称是序列号。上面的文章向您展示了如何找到这个数字和git命令行魔术,将其拉入您想要的任何分支名称的本地仓库中。

我找不到一种类似的简单方法将pull请求合并到我在GitHub上创建的fork中。


53
投票
git pull origin pull/28/head

要么

git fetch origin pull/28/head:28
git checkout 28

Can I pull a not-yet-merged pull request?


18
投票

你可以这样做:

1)添加上游远程:

git remote add upstream [email protected]:Particular/NServiceBus.git

2)之后,您可以根据其ID检查对新分支的任何拉取请求:

git fetch upstream pull/PULL_REQUEST_ID/head:NEW_BRANCH_NAME

然后你将有一个名为NEW_BRANCH_NAME的分支包含PR代码。

添加别名:

如果你经常这样做,你可能想为它设置some aliases。我在.gitconfig中有这个:

[alias]
    fetch-pr = "!f(){\
        [ -z \"$1\" ] && { echo Usage: git fetch-pr PULL_REQUEST_ID [REMOTE_NAME] [NEW_BRANCH_NAME]; exit 1; }; \
        remote=${2:-origin}; \
        branch=${3:-pr-$1}; \
        git fetch $remote \"pull/$1/head:$branch\"; \
        }; f "
    pr = "!f(){\
        branch=${3:-pr-$1}; \
        git fetch-pr \"$@\"; \
        git switch $branch; \
        }; f "

有了上述,我可以这样做:

git fetch-pr 123              # fetch PR #123 into branch pr-123
git fetch-pr 123 some-branch  # fetch PR #123 into some-branch
git pr 123                    # fetch and switch to the branch

8
投票

对于困难的情况(特别是如果你没有签出git-repo),我认为最简单的方法是应用补丁。为此,只需在github上打开pull-request并在URL中添加“.patch”,下载并应用补丁。

例:

cd cordova-plugin-media
wget https://github.com/apache/cordova-plugin-media/pull/120.patch
patch -p1 < 120.patch

5
投票

4
投票

github上/集线器

https://github.com/github/hub是一个GitHub CLI帮助程序,可以使用GitHub API中的额外信息精确地处理此用例和其他用例。例如。:

git clone https://github.com/github/hub
# Just copy paste the URL.
hub checkout https://github.com/github/hub/pull/970

结果:

  • 我们现在在一个名为<USERID>-<BRANCH_NAME>的分支上,其中包含PR。 请注意为我们自动设置的好分支名称。
  • 该分支设置为跟踪fork上的原始分支,即.git/config包含: [branch "<USERID>-<BRANCH_NAME>"] remote = retronym merge = refs/heads/ticket/969 rebase = true 因此,如果进一步的提交被推动,我们可以直接git fetch他们。

如果你不熟悉Go,那么在Linux上安装hub目前很痛苦,但值得。在Ubuntu 14.04上,存储库中的Go太旧了,所以GVM是最好的选择:

bash < <(curl -LSs 'https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer')
. "$HOME/.gvm/scripts/gvm"
gvm install 'go1.4'
gvm use 'go1.4' --default
go get github.com/github/hub

我还要求GitHub在网页用户界面上给我们一份复制粘贴备忘单:https://github.com/isaacs/github/issues/449


3
投票

将上游仓库添加为上游远程(如@elias所指出的):

$ git remote add upstream [email protected]:Particular/NServiceBus

您可以配置git默认提取拉取请求:

$ git config --local --add remote.upstream.fetch '+refs/pull/*/head:refs/remotes/upstream/pr/*'

所以,让我们来取它:

$ git fetch upstream
Fetching upstream
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/Particular/NServiceBus
 * [new ref]         refs/pull/1/head -> upstream/pr/1
 * [new ref]         refs/pull/2/head -> upstream/pr/2

检查一下:

$ git checkout pr/2
Branch pr/2 set up to track remote branch pr/2 from upstream.
Switched to a new branch 'pr/2'

1
投票

这是对我有用的命令。

我假设一个人已经在本地克隆了一个回购(例如pytorch)到他/她的系统。之后,一些志愿者/爱好者贡献了一些代码并向远程存储库发布了PR,但它还没有合并到主服务器或任何其他分支。所以,

首先,我们必须对github远程存储库执行git remote add

# I've given the name `original`; you can give some other name as per your liking
$ git remote add original https://github.com/pytorch/pytorch

然后cd进入存储库pytorch,然后简单地做:

# after this, the unmerged PR should be pulled to your local repo
$ git fetch original pull/<pull_number>/head    # 23, 123 etc.,

现在,挂起的PR已被提取到您的本地仓库中,并且您的提取的提示将在FETCH_HEAD中。如果要在本地合并此挂起的PR,则只需执行以下操作:

$ git merge FETCH_HEAD

在此之后,如果你这样做:

$ git status

您应该能够看到本地仓库领先于作为待处理PR的一部分的n提交(即,可以在单个PR中发出多于1个提交)。因此,提交的数量取决于待处理PR中包含的提交。


0
投票

下面是使用'git fetch'命令在运行“git fetch”时获取所有pull请求

在〜/ .gitconfig中添加以下内容

[remote "origin"]
fetch = +refs/pull-requests/*/from:refs/remotes/origin/pr/*

注意引用“refs / pull-requests /”有stash命名约定,对于git hub,你可能需要不同的格式

© www.soinside.com 2019 - 2024. All rights reserved.