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?
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.
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.
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> .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With