DEV Community

Maria Yudina
Maria Yudina

Posted on

Git errors: cannot checkout branch - error: pathspec 'branch_name' did not match any file(s) known to git

Sometimes after repository checkout you can encounter the error trying to switch branches:

git checkout branch_name
error: pathspec 'branch_name' did not match any file(s) known to git
Enter fullscreen mode Exit fullscreen mode

To fix that you can remove remote origin and link it again.
First, check the remote origin:

git remote -v
origin  git@github.com:company/project_name (fetch)
origin  git@github.com:company/project_name (push)
Enter fullscreen mode Exit fullscreen mode

Then remove origin:

git remote remove origin
Enter fullscreen mode Exit fullscreen mode

And add remote origin again with correct path from your repository (copy from GitHub/GitLab/etc.):

git remote add origin git@github.com:company/project_name.git
Enter fullscreen mode Exit fullscreen mode

After that run:

git pull --ff-only
Enter fullscreen mode Exit fullscreen mode

And set upstream to origin branch:

git branch --set-upstream-to=origin/current_branch
Enter fullscreen mode Exit fullscreen mode

After this you should be bale to switch between the branches as usual.

Discussion (0)