使用GitHub拉取请求的正确git配置是什么?

时间:2016-11-17 18:49:06

标签: git github pull-request

我知道How can I check out a GitHub pull request?

.git/config添加到[remote "origin"] url = https://github.com/the/repo.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/pull/*/head:refs/remotes/origin/pr/* 确实允许提取和结帐,拉动操作失败:

$ git fetch origin

获取和结帐工作正常:

$ git checkout -b "pr-123" origin/pr/123
Branch pr-123 set up to track remote branch pr/123 from origin.
Switched to a new branch 'pr-123'

......一切都很好

$ git pull
Your configuration specifies to merge with the ref 'refs/heads/pr/123' 
from the remote, but no such ref was fetched.

...成功,得到了代码!

但拉动失败了:

$ git pull origin refs/pull/123/head

...失败。

我可以手动指定参考:

[branch "pr-123"]
    remote = origin
    merge = refs/heads/pr/123

这是有效的。但是如何配置配置文件以便:

  1. 取指&结账仍然有效,
  2. 后续拉动操作无需手动指定远程参考?
  3. 我发现如果我编辑配置文件并更改:

    [branch "pr-123"]
        remote = origin
        merge = refs/pull/123/head  # <-- here is the change
    

    为:

    git pull

    ...然后re工作正常。如果不为每个拉取请求手动编辑配置文件,如何实现这一目标?

4 个答案:

答案 0 :(得分:8)

您只提取了分支,而不是提取请求。将其添加到您的配置中:

fetch = +refs/pull/*/head:refs/pulls/origin/pr/*

之后你可以签出一个指向PR远程ref的分支:

git checkout -b "pr-123" pulls/origin/pr/123

一般情况下,如果你从遥控器中取出了它,你可以查看一个引用,所以查看git fetch命令输出并找到PR的引用名称。这就是你应该放在checkout命令中的内容。你应该看到类似的东西:

[new ref] refs/pull/123/head -> refs/pulls/origin/pr/123

请注意,您可以将pulls部分替换为任何自定义前缀。您现在可以创建一个分支并将其指向pulls/origin/pr/123,这相当于refs/pulls/origin/pr/123(请参阅git refspec doc)。

答案 1 :(得分:3)

从获取规范中无法明确找到远程引用refs/remotes/origin/pr/123跟踪origin:refs/pull/123/head,因为origin:refs/heads/pr/123也是可能的。为了提供帮助,您可以使用不同的远程名称,例如:

[remote "origin-pr"]
  url = <same as for origin>
  fetch = +refs/pull/*/head:refs/remotes/origin-pr/pr/*

然后使用显式分支名称(应该在GUI中可用)的git checkout将能够创建正确的跟踪引用:

$ git checkout -b pr/123 origin-pr/pr/123

[branch "pr/123"]
 remote = origin-pr
 merge = refs/pull/123/head

虽然看起来不可能简单地git checkout br/123工作:

$ git checkout pr/123                         
error: pathspec 'pr/123' did not match any file(s) known to git.

答案 2 :(得分:2)

一种正确的方法是使用hub:)

$ brew install hub
$ hub checkout https://github.com/github/hub/pull/123

...

$ hub pull
Already up-to-date.

它有额外的工具来处理Github拉取请求,例如

hub pull-request

答案 3 :(得分:1)

我认为我找到了一个解决方案,而且简单得令人难以置信:fetch = +refs...行的顺序很重要!

我改变了:

[remote "origin"]
    url = https://github.com/the/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

为:

[remote "origin"]
    url = https://github.com/the/repo.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
    fetch = +refs/heads/*:refs/remotes/origin/*

(交换最后两行)

现在一切正常(fetchcheckoutpull)。

我还在等待看到这个配置存在一些(未)预期的问题,但到目前为止还是那么好......

相关问题