Tag Archives: Summary of Git Learning

pathspec ‘master’ did not match any files known to git

The scene that emerges
Init init a warehouse locally, then create a develop branch, and do file operations on this branch, followed by the changes made by commit.

$ git init
Initialized empty Git repository in D:/practice/testBranch/.git/
$ git checkout -b develop
Switched to a new branch 'develop'
$ vim a.txt
$ git add a.txt
$ git commit -m "add a new file"
[develop (root-commit) f9ac3b8] add a new file
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

And then you go to the master branch, and you do the file manipulation. This is where the following error occurs:

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

The cause of the problem
The command parsing
By default, the git init command creates a master branch and points the HEAD(which is a special pointer to the current local branch) to that branch. However, when you view local and remote branches using the git branch-a command, you won’t see any branches.
git checkout master command actually does two things: one is to make HEAD point back to master branch; The other is to restore the working directory to the snapshot content pointed to by the master branch.
Problem analysis
After the HEAD refers back to the master branch, the working directory needs to be restored to the content pointed to by the master branch. But since you’ve been working on the develop branch since the beginning, the working directory corresponding to the master branch is as good as nothing, so you can’t match any files.
How to solve
You just need to initialize a warehouse and first do some commit on the master branch, such as adding a readme.md file, thus creating a master branch. Such as:

$ git init
Reinitialized existing Git repository in D:/practice/testBranch/.git/
$ vim README.md
$ git add README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
$ git commit -m "add a new file"
[master (root-commit) 0e8c7c3] add a new file
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/benben/testBranch/pull/new/master
remote:
To github.com:benben/testBranch.git
 * [new branch]      master -> master

When you do a push operation, you will see that a master branch has been created in the remote warehouse, and the local master branch points to the remote master branch.
then you can see all local and remote branches by using git branch -a. You can then create additional branches and switch between master branches at will.

$ git branch -a
* master
  remotes/origin/master

When switching branches, make sure that the files in your working directory are changed. If you switch to an older branch, your working directory will revert to the way it looked the last time the branch was committed. If Git cannot do this cleanly, it will forbid switching branches.