如何在git中使用分支跟踪引用?

new*_*guy 5 git github

我希望在更新请求时使用git在本地跟踪Github拉取请求(例如:添加更多提交)。Github提供了一种方法,您可以通过检出只读refs / pull /来处理回购请求中的请求。

换句话说,如果有人向我的仓库提交了拉取请求,我可以在本地检索该请求:

git fetch upstream pull/123/head:123
git checkout 123
Run Code Online (Sandbox Code Playgroud)

当有人随后更新该拉取请求时,就会出现问题。我无法弄清楚如何更新本地分支,因为git pull无法更新本地副本:

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=<remote>/<branch> 123
Run Code Online (Sandbox Code Playgroud)

我试过了:

git branch --set-upstream-to=upstream/refs/pull/123/head 123
git branch --set-upstream-to=upstream/refs/pull/123 123
git merge --ff-only refs/pull/123/head
merge: refs/pull/123/head - not something we can merge
Run Code Online (Sandbox Code Playgroud)

Von*_*onC -2

 git fetch origin pull/123/head:123
Run Code Online (Sandbox Code Playgroud)

如果这有效,则意味着您正在从远程获取origin,而不是从远程获取upstream

为了链接此 fetch 创建的 123 本地分支,您需要:

 git branch --set-upstream-to=origin/pull/123 123
Run Code Online (Sandbox Code Playgroud)

如果有人更新了该 PR,请检查该 PR 是否没有变基。
如果是,您将需要获取 PR 分支并重置本地分支。

git fetch
git reset --hard 123 origin/pull/123
Run Code Online (Sandbox Code Playgroud)