DEV Community

Maria Yudina
Maria Yudina

Posted on • Updated 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 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.

Oldest comments (8)

Collapse
 
ks2476 profile image
Kumar Saheb • Edited

Great Post , Thanks a lot . I have been facing this issue from almost an year

Collapse
 
mariayudina profile image
Maria Yudina

I'm glad it helped!

Collapse
 
bathusha07 profile image
bathusha07

git branch --set-upstream-to=origin/current_branch can u give the sample code for this comment for the understanding

Collapse
 
mariayudina profile image
Maria Yudina

All commands are suppose to be executed in your project repository. If you were on main branch it would be git branch --set-upstream-to=origin/main

Collapse
 
raihansikder profile image
Raihan Sikder

Thanks. Saved my day!

Collapse
 
ibrahim011 profile image
Ibrahim Chaudhry

accidentally deleted my data on the git command how is it possible to recover my data

Collapse
 
binaspro profile image
binaspro

Thanks dude!

Collapse
 
leofgaliberti profile image
leofgaliberti

Hello,

Im having this same issue while trying to commit and push changed files using Azure Devops pipeline.

Unfortunately the commands described did not help. Any recommendation?
thanks in advance!