如何使用 git 检查 GitHub 拉取请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27567846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:46:24  来源:igfitidea点击:

How can I check out a GitHub pull request with git?

gitgithubpull-requestgit-checkout

提问by GarfieldKlon

I'd like to check out a previously created pull request (created via GitHub web interface). I searched and found different places where a refs/pull or refs/pull/pr

我想查看之前创建的拉取请求(通过 GitHub 网络界面创建)。我搜索并找到了 refs/pull 或 refs/pull/pr 的不同地方

But when I add fetch = +refs/pull/*/head:refs/remotes/origin/pr/*to the git config file and do a git fetch

但是当我添加fetch = +refs/pull/*/head:refs/remotes/origin/pr/*到 git 配置文件并执行 git fetch

What I'm doing wrong? Should GitHub create automatically the pull/xyz stuff, or do I have to configure something?

我做错了什么?GitHub 应该自动创建 pull/xyz 的东西,还是我必须配置一些东西?

回答by timbo

To fetch a remote PR into your local repo,

要将远程 PR 提取到您的本地存储库中,

git fetch origin pull/ID/head:BRANCHNAME

where IDis the pull request id and BRANCHNAMEis the name of the new branch that you want to create. Once you have created the branch, then simply

哪里ID是拉取请求 ID,BRANCHNAME是您要创建的新分支的名称。创建分支后,只需

git checkout BRANCHNAME

See the official GitHub documentationfor more.

有关更多信息,请参阅官方 GitHub 文档

回答by Steven Penny

This will fetch without you having to name a branch:

这将无需您命名分支即可获取:

git pull origin pull/939/head

How do I get a specific pull request on my machine?

如何在我的机器上获得特定的拉取请求?

回答by VonC

That gistdoes describe what happend when you do a git fetch:

该要点确实描述了执行 git fetch 时发生的情况:

Obviously, change the github url to match your project's URL. It ends up looking like this:

显然,更改 github url 以匹配您项目的 URL。它最终看起来像这样:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:joyent/node.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Now fetch all the pull requests:

现在获取所有拉取请求:

$ git fetch origin
From github.com:joyent/node
 * [new ref]         refs/pull/1000/head -> origin/pr/1000
 * [new ref]         refs/pull/1002/head -> origin/pr/1002
 * [new ref]         refs/pull/1004/head -> origin/pr/1004
 * [new ref]         refs/pull/1009/head -> origin/pr/1009
...

To check out a particular pull request:

要检查特定的拉取请求:

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

You have various scripts listed in issues 259to automate that task.
The git-extrasproject proposes the command git-pr(implemented in PR 262)

问题 259 中列出了各种脚本来自动执行该任务。
混帐额外的项目提出了命令git-pr(在实施PR 262

git-pr(1) -- Checks out a pull request locally

git-pr(1) -- 在本地检出一个拉取请求

SYNOPSIS

概要

git-pr <number> [<remote>]
git-pr clean

DESCRIPTION

Creates a local branch based on a GitHub pull request number, and switch to that branch afterwards.

The name of the remote to fetch from. Defaults to origin.

EXAMPLES

This checks out the pull request 226from origin:

描述

根据 GitHub 拉取请求编号创建本地分支,然后切换到该分支。

要从中获取的遥控器的名称。默认为origin.

例子

这会检查226来自origin以下位置的拉取请求:

$ git pr 226

remote: Counting objects: 12, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 3), reused 9 (delta 3)
Unpacking objects: 100% (12/12), done.
From https://github.com/visionmedia/git-extras
  * [new ref] refs/pull/226/head -> pr/226
Switched to branch 'pr/226'

回答by Andrew Ymaz

I prefer to fetch and checkout without creating a local branchand to be in HEAD detached state. It allows me quickly to check the pull request without polluting my local machine with unnecessary local branches.

我更喜欢在不创建本地分支的情况下获取和结帐并处于HEAD 分离状态。它允许我快速检查拉取请求,而不会用不必要的本地分支污染我的本地机器。

git fetch upstream pull/ID/head && git checkout FETCH_HEAD

git fetch upstream pull/ID/head && git checkout FETCH_HEAD

where IDis a pull request ID and upstreamwhere is original pull request has been created (it could be origin, for example).

哪里ID是拉取请求 ID,upstream在哪里创建了原始拉取请求(origin例如,可以是)。

I hope it helps.

我希望它有帮助。

回答by bholagabbar

Referencing Steven Penny's answer, it's best to create a test branch and test the PR. So here's what you would do.

参考 Steven Penny 的回答,最好创建一个测试分支并测试 PR。所以这就是你要做的。

  1. Create a test branch to merge the PR into locally. Assuming you're on the master branch:
  1. 创建一个测试分支,将 PR 合并到本地。假设你在 master 分支上:

git checkout -b test

git checkout -b test

  1. Get the PR changes into the test branch
  1. 获取 PR 更改到测试分支

git pull origin pull/939/head:test

git pull origin pull/939/head:test

Now, you can safely test the changes on this local test branch (in this case, named test) and once you're satisfied, can merge it as usual from GitHub.

现在,您可以安全地测试此本地测试分支(在本例中名为test)上的更改,一旦您满意,就可以像往常一样从 GitHub 合并它。

回答by AmitB

If you are using Github.com, go to "Pull requests", click on the relevant pull request, and then click on the "command line instructions" link: command line instructions at Github.com

如果您使用的是 Github.com,请转到“拉取请求”,点击相关拉取请求,然后点击“命令行说明”链接: Github.com 上的命令行说明

回答by Tomas Tomecek

You can use git configcommand to write a new rule to .git/configto fetch pull requests from the repository:

您可以使用git config命令编写新规则以.git/config从存储库中获取拉取请求:

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

And then just:

然后只是:

$ git fetch origin
Fetching origin
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/container-images/memcached
 * [new ref]         refs/pull/2/head -> origin/pr/2
 * [new ref]         refs/pull/3/head -> origin/pr/3

回答by Ariel Gabizon

The problem with some of options above, is that if someone pushes more commits to the PR after opening the PR, they won't give you the most updated version. For me what worked best is - go to the PR, and press 'Commits', scroll to the bottomto see the most recent commit hash enter image description hereand then simply use git checkout, i.e.

上面一些选项的问题是,如果有人在打开 PR 后向 PR 推送更多提交,他们不会为您提供最新版本。对我来说最有效的是 - 转到 PR,然后按“提交”,滚动到底部以查看最近的提交哈希 在此处输入图片说明,然后简单地使用 git checkout,即

git checkout <commit number>

git checkout <commit number>

in the above example

在上面的例子中

git checkout 0ba1a50

git checkout 0ba1a50

回答by Daniel Wehner

I'm using hub, a tool from github: https://github.com/github/hub

我正在使用 hub,一个来自 github 的工具:https: //github.com/github/hub

With hub checking out a pull request locally is kinda easy:

使用集线器在本地检查拉取请求有点容易:

hub checkout https://github.com/owner/repo/pull/1234
or
hub pr checkout 1234

回答by osexp2003

For Bitbucket, you need replace the word pullto pull-requests.

对于 Bitbucket,您需要将单词替换pullpull-requests

First, you can confirm the pull request URL style by git ls-remote origincommand.

首先,您可以通过git ls-remote origin命令确认拉取请求 URL 样式。

$ git ls-remote origin |grep pull
f3f40f2ca9509368c959b0b13729dc0ae2fbf2ae    refs/pull-requests/1503/from
da4666bd91eabcc6f2c214e0bbd99d543d94767e    refs/pull-requests/1503/merge
...

As you can see, it is refs/pull-requests/1503/frominstead of refs/pull/1503/from

如您所见,它refs/pull-requests/1503/from不是refs/pull/1503/from

Then you can use the commands of any of the answers.

然后你可以使用任何答案的命令。