git refs/pull/... vs. pull/

1.5k views Asked by At

In various places, looking for instructions on how to create a local branch from a GitHub Pull Request, I have seen two versions:

git fetch upstream refs/pull/PR_ID/head:NEW_LOCAL_BRANCH

and

git fetch upstream pull/PR_ID/head:NEW_LOCAL_BRANCH

My question is, what's the difference between including "refs" in the address, and not including it?
Both seem to work fine.

2

There are 2 answers

0
matt On BEST ANSWER

what's the difference between including "refs" in the address, and not including it?

Including it gives a reference that works, directly.

Not including it means that Git has to figure out what you mean. It does that by trying stuff. One of the first things it tries is to put refs/ in front of what you said — and by golly, that works.


(For a recital of the full procedure that Git uses to figure out what you mean, see

https://git-scm.com/docs/gitrevisions

Look at the third entry under Specifying Revisions.)

2
torek On

The prefix refs/pull/ is a ref (or reference) namespace invented—if that word isn't too strong—by GitHub to use for their Pull Request feature. It's not a standard Git namespace, and Git by default will ignore names in the refs/pull/ namespace when fetching.

As matt noted, if you use an abbreviated ref (anywhere, not just in a refspec), Git will attempt to match it up with a full ref if possible. The precise rules for doing this matching depend on where you use such a name. The usual set is outlined in the gitrevisions documentation, but it's talking about local refs that exist only in your own repository; when you're doing git fetch or git push, some refs are not local and these rules get bent up a little.