Why am I unable to create/checkout this branch?

2019-02-20 17:16发布

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?

2条回答
时光不老,我们不散
2楼-- · 2019-02-20 17:23

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'
查看更多
SAY GOODBYE
3楼-- · 2019-02-20 17:29

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
查看更多
登录 后发表回答