Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

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
like image 755
newguy Avatar asked Feb 28 '16 23:02

newguy


People also ask

How do I track a specific branch?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".

What is tracking branch in git?

Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git pull , Git automatically knows which server to fetch from and which branch to merge in.

How do I find a specific branch in git?

There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.

How do I checkout and track a remote branch?

Use the Git Fetch command to fetch the remote branch that you want to checkout. We can either mention the name of the remote branch or fetch all the remote branches present in the remote repository.


1 Answers

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.

like image 199
bartgol Avatar answered Oct 03 '22 00:10

bartgol