Why am I unable to create/checkout this branch?

橙三吉。 提交于 2019-12-01 11:07:49

问题


I am trying to create a local Git branch, but it is not working. Here are the commands I am using:

tablet:edit11$ git checkout -b edit_11
Switched to a new branch 'edit_11'
tablet:edit11$ git checkout edit_11
error: pathspec 'edit_11' did not match any file(s) known to git.
tablet:edit11$ git branch
tablet:edit11$

What's going on?


回答1:


You successfully created and "switched to" a branch called edit_11 when you ran

git checkout -b edit_11

However, everything (incl. an empty git branch output) indicates that you have just initialised your repository and have yet made to make an initial commit. If there is no commit, branches have nothing useful to point at and there is nothing to check out.

Therefore, when you run

git checkout edit_11

you get the following error,

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

even though branch edit_11 does exists.


The problem can be reproduced as follows:

$ mkdir testgit
$ cd testgit
$ git init
Initialized empty Git repository in /xxxx/testgit/.git/
$ git checkout -b edit_11
Switched to a new branch 'edit_11'
$ git checkout edit_11
error: pathspec 'edit_11' did not match any file(s) known to git.
$ git branch
$

After you make a first commit on branch edit_11, git checkout edit_11 will not longer throw any error. Note that this it is a no-op, here, because the current branch is already edit_11.

$ printf foo > README
$ git add README
$ git commit -m "add README"
[edit_11 (root-commit) 90fe9c1] add README
 1 file changed, 1 insertion(+)
 create mode 100644 README
$ git branch
* edit_11
$ git checkout edit_11 
Already on 'edit_11'



回答2:


The first line creates the new branch and moves you to that branch. You doesn't need to do anymore. Just only use:

git checkout -b edit_11


来源:https://stackoverflow.com/questions/32109985/why-am-i-unable-to-create-checkout-this-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!