frnhr frnhr - 13 days ago 7
Git Question

What is the correct git config for working with GitHub pull requests?

I'm aware of How can I check out a GitHub pull request?

While adding

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
to
.git/config
does allow fetch and checkout, pull actions fail:

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


Fetch and checkout work fine:

$ git fetch origin


... all good

$ 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'


... success, got the code!

But pull fails:

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


... failed.

I can specify the ref manually:

$ git pull origin refs/pull/123/head


and this works. But how can I configure the config file so that:


  1. fetch & checkout still work, and

  2. subsequent pull actions work without manually specifying the remote ref?






I have found that if I edit the config file and change:

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


to:

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


... then
git pull
works fine. How can this be achieved without manually editing the config file for every pull request?

Answer

I think I found a solution, and it's unbelievably simple: Order of the lines with fetch = +refs... matters!

I changed:

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

to:

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

(last two lines swapped)

Now everything works (fetch, checkout, pull).

I'm still waiting to see some (un)expected problems with this configuration, but so far so good...