Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout remote reference

Tags:

git

github

I have a list of pull requests on github. I can fetch the pull requests like this:

git fetch origin +refs/pull/*:refs/remotes/origin/pr/*

I get output like this:

* [new ref]         refs/pull/1/head -> origin/pr/1/head
* [new ref]         refs/pull/1/merge -> origin/pr/1/merge
* [new ref]         refs/pull/10/head -> origin/pr/10/head
* [new ref]         refs/pull/10/merge -> origin/pr/10/merge
* [new ref]         refs/pull/11/head -> origin/pr/11/head
* [new ref]         refs/pull/11/merge -> origin/pr/11/merge

Now I want to checkout one of those refs. Nothing I try seems to work:

$ git checkout refs/pull/1/head
error: pathspec 'refs/pull/1/head' did not match any file(s) known to git.

Or:

git checkout origin/pr/1/head
error: pathspec 'origin/pr/1/head' did not match any file(s) known to git.

How can I checkout this reference?

like image 726
Herman J. Radtke III Avatar asked Nov 30 '12 02:11

Herman J. Radtke III


People also ask

Does git checkout pull from remote?

Git Checkout a Remote Branch Each remote repository will contain its own set of branches. In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch.

What is remote reference in git?

Remote references are references (pointers) in your remote repositories, including branches, tags, and so on. You can get a full list of remote references explicitly with git ls-remote <remote> , or git remote show <remote> for remote branches as well as more information.

How do I checkout someone else's remote branch?

If you want to check out a remote branch someone published, you first have to use git fetch . This command downloads the references from your remote repository to your local machine, including the reference to the remote branch. Now all you need to do is use git checkout <remote branch name> .


1 Answers

The first command (git checkout refs/pull/1/head) didn't work because refs/pull/1/head is the name of the reference in the remote repository. You don't have a reference with that name in your local repository because your fetch refspec translated it to refs/remotes/origin/pr/1/head.

The second command (git checkout origin/pr/1/head) should have worked, although it should have given you a "detached HEAD" warning. Was there a typo that you fixed when posting your question to Stack Overflow?

Your fetch refspec told git to translate the remote references into local references in the refs/remotes directory. The references in that directory are treated specially -- they're "remote references" meant to indicate the state of the remote repository the last time you did a fetch. Normally you don't want to check those refs out directly -- you want to create a local branch that is configured to "follow" or "track" the remote reference (which enables special convenience shortcuts such as the @{u} revision parameter and easier push/pull usage).

Try:

git fetch origin +refs/pull/*:refs/remotes/origin/pr/*
git checkout -b whatever-branch-name-you-want origin/pr/1/head

The above creates a new local branch called whatever-branch-name-you-want (I recommend calling it pr/1/head) pointing at the same commit as origin/pr/1/head, configures whatever-branch-name-you-want to track origin/pr/1/head, then switches to the new branch.

like image 100
Richard Hansen Avatar answered Oct 03 '22 23:10

Richard Hansen