6

I want to track a Github pull request locally with git as the request is updated (example: more commits are added). Github has a way that you can work on pull requests that come to your repo by checking out the read only refs/pull/.

In other words if someone submits a pull request to my repo I can retrieve that request locally:

git fetch upstream pull/123/head:123
git checkout 123

The problem comes in when someone then updates that pull request. I can't figure out how to update the local branch because git pull doesn't work to update my local copy:

$ 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

I've tried:

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
newguy
  • 537
  • 4
  • 12
  • It looks like you referenced the wrong upstream branch, I would have expected something like `git branch --set-upstream-to=origin/pull/123`. You can check all branch references with `git branch -a` after you fetched from remote. – morxa Feb 29 '16 at 01:14
  • You can also run `git branch -a -vv` to check whether your local 123 branch is tracking remote branch. – gzh Feb 29 '16 at 01:28
  • @morxa I tried that but it doesn't work – newguy Feb 29 '16 at 04:57
  • What do you mean with "it doesn't work"? What's the output? – morxa Feb 29 '16 at 04:58
  • @morxa error: the requested upstream branch 'upstream/pull/123' does not exist – newguy Feb 29 '16 at 06:46
  • Your remote is called `upstream`? What does `git branch -a` show? – morxa Feb 29 '16 at 06:47
  • @morxa It shows a number of branches for remotes/upstream but it doesn't show any of the pull refs. – newguy Feb 29 '16 at 06:56
  • I've had the same problem and I've detected the branch and create one again using `git fetch`, since it's remote it mean you can't push there so delete and fetch is the same as pull. – jcubic Aug 30 '18 at 18:26

2 Answers2

1

I had the same problem, and thought I'd share how I solved it.

The idea of using --set-upstream-to does not work, cause the remote reference does not seem to be a branch. That sounds weird, but I think it has to to with where the ref is stored remotely (branches are in refs/heads, but pull requests are in refs/pull).

However, you can add a refspec to your fetch rule for your remote in your git config

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

You can even add a git alias that adds a single pr number to your refspecs in your gitconfig (and maybe an alias to remove it when you're done). You can use whatever you want after origin, but this is a common choice.

With this, when you do git fetch origin, git will also update the ref of the pr. Almost done:

git checkout -b pr/${prnum} --track origin/pr/${prnum}

Done! Now your local branch pr/123 tracks remotes/origin/pr/123. The latter is a name only valid in your local repo, but it is kept in sync with the pull request branch every time you do a git fetch (including when you do git pull).

Note: a not too old git version should also allow to replace the checkout command above with

git checkout pr/123

which will create the branch pr/123, and correctly set up the tracking for the ref remotes/origin/pr/123.

bartgol
  • 1,443
  • 18
  • 22
-1
 git fetch origin pull/123/head:123

If this work, that means you are fetching from remote origin, not remote upstream.

In order to link the 123 local branch created by this fetch, you would need:

 git branch --set-upstream-to=origin/pull/123 123

And if someone update that PR, check if said PR was not rebase.
If it is, you would need to fetch the PR branch and reset the local branch.

git fetch
git reset --hard 123 origin/pull/123
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • I'm sorry about that I meant the branch is named upstream I have edited my post to fix that. The problem is the fetch command works for example `git fetch upstream pull/123/head:123` to get a pull request but `git branch --set-upstream-to=upstream/pull/123 123` does not, it shows *error: the requested upstream branch 'upstream/pull/123' does not exist*. – newguy Feb 29 '16 at 20:28
  • Upstream is a remote name not a branch name – VonC Feb 29 '16 at 20:29
  • What does git branch -avv return? – VonC Feb 29 '16 at 20:30
  • Yeah I meant the remote is named upstream. `git branch -avv` shows no tracking for the 123 branch, and also there is a bunch of `remotes/upstream/` branches but nothing for pull/123 – newguy Feb 29 '16 at 20:56
  • That is the issue then: is there a pull/123 branch at the remote repo upstream? – VonC Feb 29 '16 at 20:57
  • Well like I said this is a github read only ref so I don't understand what's happening. In other words github creates a pull//head and it works to do `git fetch upstream pull/123/head:123` and then I can locally checkout 123 but I can't use --set-upstream-to to set the upstream. – newguy Feb 29 '16 at 21:00
  • I mean: clone that upstream repo elsewhere, and do a git br -avv: do you see a feature/pull/123 in that new cloned repo? – VonC Feb 29 '16 at 21:02
  • No I do not see that. As I said I can fetch that ref but I can't track it. Is it possible my git is too old? `git version 1.9.5.msysgit.1` – newguy Feb 29 '16 at 21:25
  • 1
    It shouldn't, but it never hurt to upgrade to 2.7.2 – VonC Feb 29 '16 at 21:26
  • Now clone again, but this time, your own github repo, the one to which one does pull request to. Does a git branch -avv shows some pull/123 there? – VonC Feb 29 '16 at 21:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104921/discussion-between-newguy-and-vonc). – newguy Feb 29 '16 at 21:32
  • Sorry, got to go to bed :( but for me, those branches should be in the upstream repo. – VonC Feb 29 '16 at 21:33
  • It is like this for any repository. For example try `git clone https://github.com/spotify/scio.git` and `cd scio` and `git fetch origin pull/20/head:20` and `git branch --set-upstream-to=origin/pull/20 20`. – newguy Mar 02 '16 at 22:20