Search code examples
gitbranchgit-checkout

Why am I unable to create/checkout this branch?


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?


Solution

  • 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, in this example, git checkout edit_11 is a no-op because the current branch already is 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'