无法从远程签出新分支

时间:2014-06-14 13:18:50

标签: git

我这样做了:

git fetch --all
Fetching origin
remote: Counting objects: 242, done.
............
From bitbucket.org:xxx/xxx
   xxxx..xxxx  develop    -> origin/develop
 * [new branch]      branch1 -> origin/branch1
   xxxx..xxxx  master     -> origin/master

然后我做了:

git checkout branch1
error: pathspec 'branch1' did not match any file(s) known to git.

但是应该检查出branch1。这有什么用呢?

2 个答案:

答案 0 :(得分:2)

没有名为branch1的本地分支。你需要说

git checkout -b branch1 origin/branch1

这将创建一个名为branch1的本地分支,用于跟踪远程分支branch1

答案 1 :(得分:1)

来自实验:

[wei2912@localhost wee-repo]$ git init
Initialized empty Git repository in /home/wei2912/tmp/wee-repo/.git/
[wei2912@localhost wee-repo]$ git checkout -b branch1
Switched to a new branch 'branch1'
[wei2912@localhost wee-repo]$ git checkout -b master
Switched to a new branch 'master'
[wei2912@localhost wee-repo]$ git checkout branch1
error: pathspec 'branch1' did not match any file(s) known to git.
[wei2912@localhost wee-repo]$ git branch -a
[wei2912@localhost wee-repo]$ 

在初始提交之前,您不能创建多个分支(如果您考虑它,这是有意义的)。您需要签出一个分支并提交到该分支,然后才能创建从初始分支(您选择提交的分支)分支出来的其他分支。

[wei2912@localhost wee-repo]$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[wei2912@localhost wee-repo]$ touch test.txt
[wei2912@localhost wee-repo]$ git add test.txt
[wei2912@localhost wee-repo]$ git branch -a
* master
[wei2912@localhost wee-repo]$ git checkout -b branch1
Switched to a new branch 'branch1'
[wei2912@localhost wee-repo]$ git branch -a
* branch1
  master

如您所见,在进行初始提交后,您可以创建分支master分支的分支。

注意:这是假设您尚未向存储库提交任何内容。如果还没有,请通知我,我会编辑/删除我的答案。