81

我需要在 NServiceBus 存储库中拉入特定的拉取请求(尚未处理到主流中):

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

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

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

4

13 回答 13

76

要将拉取到您的存储库中:

git fetch git@github.com:jboss/jboss-common-beans.git refs/pull/4/head

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

git checkout -b new-branch FETCH_HEAD
于 2012-03-28T07:48:42.603 回答
75
git pull origin pull/28/head

或者

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

我可以拉一个尚未合并的拉取请求吗?

于 2015-09-07T17:26:10.843 回答
25

你可以这样做:

1)添加上游远程:

git remote add upstream git@github.com:Particular/NServiceBus.git

2) 之后,您可以根据其 ID 将任何拉取请求签出到新分支:

git fetch upstream pull/PULL_REQUEST_ID/head:NEW_BRANCH_NAME

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

添加别名:

如果您像我一样经常这样做,您可能需要为其设置一些别名。我的 .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
于 2015-03-10T00:34:15.957 回答
9

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

例子:

cd cordova-plugin-media
wget https://github.com/apache/cordova-plugin-media/pull/120.patch
patch -p1 < 120.patch
于 2016-11-17T11:56:10.960 回答
5

请参阅 GitHub 中的此帮助文章:https ://help.github.com/articles/checking-out-pull-requests-locally

于 2014-05-21T23:44:27.450 回答
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直接提交。

hub如果您不熟悉 Go,目前在 Linux 上安装会很痛苦,但值得。在 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 在 Web UI 上给我们一份复制粘贴备忘单:https ://github.com/isaacs/github/issues/449

于 2015-08-17T10:33:56.767 回答
3

一旦您将上游仓库添加为上游远程(正如@elias 指出的那样):

$ git remote add upstream git@github.com: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'
于 2017-07-04T07:02:03.747 回答
3

这是对我有用的命令。

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

首先,我们必须git remote add对 github 远程存储库进行操作:

# 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 已被提取到您的本地 repo 中,并且您的提取提示将在 FETCH_HEAD 中。如果您想在本地合并这个待处理的 PR ,那么只需执行以下操作:

$ git merge FETCH_HEAD

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

$ git status

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

于 2019-04-11T13:08:26.590 回答
2

这是来自 GitHub 文档的解决方案:

从您的项目存储库中,签出一个新分支并测试更改。

git checkout -b GithubUserID-branchName branchName
git pull https://github.com/GithubUserID/reponame.git branchName

在哪里:

  1. GithubUserID是打开拉取请求的人的用户名。
  2. branchName是分支的名称,例如 master
  3. reponame存储库名称,例如 demo-todo

例子 :

git checkout -b felix123-master master
git pull https://github.com/felix123/demo-todo.git master
于 2020-06-26T16:22:50.660 回答
1

如果您只想将来自其他仓库的一个未合并的拉取请求添加到您自己的仓库中,则不需要所有复杂性(如其他答案中所示)。

相反,只需进入您自己的 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
于 2018-11-02T21:30:32.053 回答
0

下面是让您的“git fetch”命令在运行“git fetch”时获取所有拉取请求

将以下内容添加到 ~/.gitconfig

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

请注意参考“refs/pull-requests/”具有存储命名约定,对于 git hub,您可能需要不同的格式

于 2017-03-28T10:34:49.387 回答
0

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

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

归结为知道 GitHub 中的拉取请求只是基础 repo 中的一个分支,其中分支名称是一个序列号。上面的文章向您展示了如何找到这个数字以及 git 命令行魔法,以使用您想要的任何分支名称将其拉入本地存储库。

我找不到类似的简单方法将拉取请求合并到我在 GitHub 上创建的分支中。

于 2019-04-20T18:40:38.997 回答
0

我找到了这个问题的解决方案 - 从不同机器上的未合并 PR 中提取更改,我在 git bash 1 上做了以下事情。您必须在机器 2 上克隆远程存储库 2. 执行 git checkout(生成 PR 的分支) 3. 执行 git pull 并完成!!!!!!!!!!!!!!!

于 2019-08-29T09:59:24.293 回答