Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 409
frnhr Avatar asked Nov 17 '16 18:11

frnhr


1 Answers

You've only fetched branches, not pull requests. Add this to your config:

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

After that you can checkout a branch that points to a PR remote ref:

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

Generally, you can check out a ref if you've fetched it from the remote, so look through the git fetch command output and find the PR's ref name. That's what you should put in the checkout command. You should see something like:

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

Note that you can substitute the pulls part for any custom prefix. You can now create a branch and point it to pulls/origin/pr/123, which is equivalent to refs/pulls/origin/pr/123 (see git refspec doc).

like image 161
yelsayed Avatar answered Oct 16 '22 23:10

yelsayed