Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: pathspec 'test-branch' did not match any file(s) known to git

I am a new user of Git. I have forked a repository called Spoon-Knife (available for practicing forking with Git). Then, I cloned it locally by running

git clone https://github.com/rohinichoudhary/Spoon-Knife.git 

This repository contains three branches, i.e.

  • master,
  • test-branch,
  • change-the-title.

When I run git branch, it only shows *master, not the remaining two branches. And when I run

git checkout test-branch 

I get the following error:

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

Why is this happening? How can I solve this problem?

like image 762
Rohini Choudhary Avatar asked Jun 12 '15 10:06

Rohini Choudhary


People also ask

How do you fix did not match any files known to git?

Git errors: cannot checkout branch - error: pathspec 'branch_name' did not match any file(s) known to git. To fix that you can remove remote origin and link it again. After this you should be bale to switch between the branches as usual.

What is Pathspec git?

The pathspec is the mechanism that git uses for limiting the scope of a git command to a subset of the repository. If you have used much git, you have likely used a pathspec whether you know it or not. For example, in the command git add README.md , the pathspec is README.md .

How do I checkout a remote local branch?

In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch. Older versions of Git require the creation of a new branch based on the remote .


2 Answers

The modern Git should able to detect remote branches and create a local one on checkout.

However if you did a shallow clone (e.g. with --depth 1), try the following commands to correct it:

git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' git fetch --all 

and try to checkout out the branch again.

Alternatively try to unshallow your clone, e.g. git fetch --unshallow and try again.

See also: How to fetch all remote branches?

like image 121
kenorb Avatar answered Sep 21 '22 20:09

kenorb


When I run git branch, it only shows *master, not the remaining two branches.

git branch doesn't list test_branch, because no such local branch exist in your local repo, yet. When cloning a repo, only one local branch (master, here) is created and checked out in the resulting clone, irrespective of the number of branches that exist in the remote repo that you cloned from. At this stage, test_branch only exist in your repo as a remote-tracking branch, not as a local branch.

And when I run

git checkout test-branch 

I get the following error [...]

You must be using an "old" version of Git. In more recent versions (from v1.7.0-rc0 onwards),

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat [git checkout <branch>] as equivalent to

$ git checkout -b <branch> --track <remote>/<branch> 

Simply run

git checkout -b test_branch --track origin/test_branch 

instead. Or update to a more recent version of Git.

like image 43
jub0bs Avatar answered Sep 22 '22 20:09

jub0bs