git

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

mariayudina

Maria Yudina

Posted on November 14, 2021

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 able to switch between the branches as usual.

This error message indicates that Git was unable to checkout the specified branch because it does not exist. This can happen for a few different reasons, including the following:

  • The branch name is misspelled or mistyped.
  • The branch has already been deleted or is no longer available.
  • The branch exists in a remote repository, but it has not yet been pulled or fetched to the local repository.

To fix this error, you will need to verify that the branch name is correct and that the branch exists in the local repository. If the branch name is correct and the branch still does not exist, you may need to pull or fetch the branch from the remote repository where it exists.

If the branch has already been deleted or is no longer available, you will need to create a new branch with a different name or switch to a different existing branch.

Overall, this error can be resolved by checking the branch name and ensuring that the branch exists in the local repository. If necessary, you can also try pulling or fetching the branch from the remote repository where it exists.

💖 💪 🙅 🚩
mariayudina
Maria Yudina

Posted on November 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related