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

frn*_*nhr 19 git github pull-request

我知道如何查看GitHub拉取请求?

虽然添加fetch = +refs/pull/*/head:refs/remotes/origin/pr/*.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/*
Run Code Online (Sandbox Code Playgroud)

获取和结帐工作正常:

$ git fetch origin
Run Code Online (Sandbox Code Playgroud)

... 都好

$ 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'
Run Code Online (Sandbox Code Playgroud)

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

但拉动失败:

$ git pull
Your configuration specifies to merge with the ref 'refs/heads/pr/123' 
from the remote, but no such ref was fetched.
Run Code Online (Sandbox Code Playgroud)

......失败了.

我可以手动指定ref:

$ git pull origin refs/pull/123/head
Run Code Online (Sandbox Code Playgroud)

这很有效.但是如何配置配置文件以便:

  1. 提取和结帐仍然有效,并且
  2. 后续拉动操作无需手动指定远程参考?

我发现如果我编辑配置文件并更改:

[branch "pr-123"]
    remote = origin
    merge = refs/heads/pr/123
Run Code Online (Sandbox Code Playgroud)

至:

[branch "pr-123"]
    remote = origin
    merge = refs/pull/123/head  # <-- here is the change
Run Code Online (Sandbox Code Playgroud)

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

yel*_*yed 8

你只获取了分支,而不是拉取请求.将其添加到您的配置:

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

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

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

通常,如果您从远程获取了ref,则可以检出引用,因此请查看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).


frn*_*nhr 4

我想我找到了一个解决方案,而且简单得令人难以置信:行的顺序与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/*
Run Code Online (Sandbox Code Playgroud)

到:

[remote "origin"]
    url = https://github.com/the/repo.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
    fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

(最后两行交换)

现在一切正常 ( fetch, checkout, pull)。

我仍在等待看到此配置的一些(意外)问题,但到目前为止一切顺利......