newguy
newguy

Reputation: 617

How can I track a reference with a branch in git?

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

Upvotes: 11

Views: 1432

Answers (2)

bartgol
bartgol

Reputation: 1873

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.

Upvotes: 5

VonC
VonC

Reputation: 1328712

 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

Upvotes: -2

Related Questions