17

我在 github 上有一个拉取请求列表。我可以像这样获取拉取请求:

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

我得到这样的输出:

* [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

现在我想检查其中一个裁判。我尝试的任何方法似乎都不起作用:

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

或者:

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

如何查看此参考资料?

4

2 回答 2

22

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.

于 2012-11-30T03:37:03.163 回答
0

Check out what's available to checkout with

git branch -a
于 2012-11-30T02:43:33.183 回答