Ramon Tayag
Ramon Tayag

Reputation: 16084

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

I'm not sure why I'm unable to checkout a branch that I had worked on earlier. See the commands below (note: co is an alias for checkout):

ramon@ramon-desktop:~/source/unstilted$ git branch -a
* develop
  feature/datts_right
  feature/user_controlled_menu
  feature/user_controlled_site_layouts
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/feature/datts_right
  remotes/origin/master
ramon@ramon-desktop:~/source/unstilted$ git co feature/user_controlled_site_layouts 
error: pathspec 'feature/user_controlled_site_layouts' did not match any file(s) known to git.

I'm not sure what it means, and I can't seem to find anything I can understand on Google.

How do I checkout that branch, and what may I have done to break this?

UPDATE:

I found this post, and running git show-ref gives me:

97e2cb33914e763ff92bbe38531d3fd02408da46 refs/heads/develop
c438c439c66da3f2356d2449505c073549b221c1 refs/heads/feature/datts_right
11a90dae8897ceed318700b9af3019f4b4dceb1e refs/heads/feature/user_controlled_menu
c889b37a5ee690986935c9c74b71999e2cf3c6d7 refs/heads/master
c889b37a5ee690986935c9c74b71999e2cf3c6d7 refs/remotes/origin/HEAD
e7c17eb40610505eea4e6687e4572191216ad4c6 refs/remotes/origin/develop
c438c439c66da3f2356d2449505c073549b221c1 refs/remotes/origin/feature/datts_right
c889b37a5ee690986935c9c74b71999e2cf3c6d7 refs/remotes/origin/master
23768aa5425cbf29d10ff24274adad42d90d15cc refs/stash
e572cf91e95da03f04a5e51820f58a7306ce01de refs/tags/menu_shows_published_only
429ebaa895d9d41d835a34da72676caa75902e3d refs/tags/slow_dev

UPDATE on .git directory (user_controlled_site_layouts is in the refs/heads/feature folder):

$ ls .git/refs/heads/feature/
datts_right  user_controlled_menu  user_controlled_site_layouts
$ cat .git/refs/heads/feature/user_controlled_site_layouts
3af84fcf1508c44013844dcd0998a14e61455034

UPDATE on git show 3af84fcf1508c44013844dcd0998a14e61455034

$ git show 3af84fcf1508c44013844dcd0998a14e61455034
commit 3af84fcf1508c44013844dcd0998a14e61455034
Author: Ramon Tayag <[email protected]>
Date:   Thu May 12 19:00:03 2011 +0800

    Removed site layouts migration

diff --git a/db/schema.rb b/db/schema.rb
index 1218fc8..2040b9f 100755
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended to check this file into your version control system.

-ActiveRecord::Schema.define(:version => 20110511012647) do
+ActiveRecord::Schema.define(:version => 20110503040056) do

   create_table "attachments", :force => true do |t|
     t.string   "name"
@@ -205,15 +205,6 @@ ActiveRecord::Schema.define(:version => 20110511012647) do
     t.integer  "old_id"
   end

-  create_table "site_layouts", :force => true do |t|
-    t.string   "name"
-    t.text     "description"
-    t.text     "content"
-    t.integer  "site_id"
-    t.datetime "created_at"
-    t.datetime "updated_at"
-  end
-
   create_table "site_styles", :force => true do |t|
     t.text     "published"
     t.datetime "created_at"

Upvotes: 1171

Views: 2270789

Answers (30)

Sumith Ekanayake
Sumith Ekanayake

Reputation: 2265

This error occurs in Git when you attempt to check out a branch that does not exist in your repository or in the remote repository you're tracking.

Here's how to troubleshoot and resolve the issue:

  • List branches again: Use git branch -a to see if the branch has been deleted or renamed.
  • Check remote branches: If you're trying to check out a remote branch, use git fetch to update your local branch list.
  • Attempt checkout: git checkout <branchName>

Upvotes: 0

Anurag Dwivedi
Anurag Dwivedi

Reputation: 121

git fetch origin <branchName>:<branchName>

git checkout <branchName>

Make sure there are no new commits done after you have run first command git push --set-upstream origin <branchName>

Upvotes: 6

Dylan_Gomes
Dylan_Gomes

Reputation: 2232

For me, I did not have a master branch, it was called main.

There are many answers here suggesting individuals had typos or they were not sure of what the branch name actually is (or in the wrong repo), but none of the answers suggest how one can go about confirming this.

git branch

will populate a list of all of the branches one has (in the current repo), with the current branch highlighted in green (at least on git bash) and preceded by an asterisk.

If the branch you are trying to switch to is in this list (and the spelling matches), then try the other answers including git fetch, etc.

Upvotes: 0

aloser01
aloser01

Reputation: 69

This problem may have been caused when doing a commit either without the commit message specified with -m or using single apostrophes ' for the message in windows terminal:

Wrong:

git commit "minor changes"

git commit -m 'minor changes'

Correct:

git commit -m "minor changes"

Upvotes: 3

MarkoHiel
MarkoHiel

Reputation: 16301

Try git fetch so that your local repository gets all the new info from Github. It just takes the information about new branches and no actual code. After that, the git checkout should work fine.

Upvotes: 1581

Fasil kk
Fasil kk

Reputation: 2187

Tried all recommended solutions and didn't work in my case, so I removed the git repo and added it again. Solved all issues.

git remote remove origin
git remote add origin [email protected]:org_name/project_name.git

Upvotes: 2

BollMose
BollMose

Reputation: 3498

A better/simpler one:

git fetch origin yourLocalBranch:yourLocalBranch
git checkout yourLocalBranch

I got this error when I shallow clone a repo, and got it a solution For example the branch I want to checkout is release/120

git ls-remote origin release/120 # make sure the remote branch exits
git fetch origin refs/heads/release/120:refs/remotes/origin/release/120 # fetch to local repo
git co -b release/120 origin/release/120 # checkout to workspace

Upvotes: 14

Pedro Coelho
Pedro Coelho

Reputation: 1617

A possible reason for this issue might be lack of synchronization between a local repository and its upstream repository too. In this case, a simple git pull upstream <branch_name> followed by a git checkout <branch_name> does the trick.

Upvotes: 1

Botnetdobbs
Botnetdobbs

Reputation: 716

Just do a git fetch origin <branchName>:<branchName>.

This will fetch and create a local copy of the same hence now you can

checkout into it git checkout <branchName>

Upvotes: 56

Niroshan Ratnayake
Niroshan Ratnayake

Reputation: 3801

This error did occur to me recently when I tried to checkout to my master branch with the below command.

git checkout master

Since Github has changed the naming convention from master to main above command will give you the error. So instead of that, you may use the below command.

git checkout main 

Upvotes: 5

qucumbah
qucumbah

Reputation: 83

I was losing my mind over this issue, but then I've read the documentation and realised that my company has split their repo into multiple remotes, so some branches would be on the main remote and others would require you to fetch from a different remote.

Check whether the repo you're trying to checkout has one remote or multiple

Upvotes: 3

Kate
Kate

Reputation: 1247

My SPECIAL CASE have the same error:

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

MY SPECIAL CASE AND MY SPECIAL CASE SOLUTION: Since Git's "master" name updated with "main", new project can only use "main".

git checkout main

Please do not use when you don't have same case with me.

Upvotes: -3

Pranay kumar
Pranay kumar

Reputation: 2197

I had the same issue for one of my branch.

These commands work for me.

git fetch --all
git checkout <branch-name>

Upvotes: 51

Putnik
Putnik

Reputation: 6774

I faced this issue when I tried to checkout using specific tag but the tag value was incorrect. Here's an example how easy it can be:

[email protected]:user/reponame?ref=1.0.0

this was incorrect and below is the correct one (note v before the version name):

[email protected]:user/reponame?ref=v1.0.0

If you're not familiar with all those tiny details it's really easy to omit them.

Upvotes: 0

Pramod KP
Pramod KP

Reputation: 188

Well, I had few deleted branches like dev/{feature_branch} and when I created a new branch dev and tried to checkout, I was getting the same issue. I ran the below command

git fetch -p

and worked for me.

Upvotes: 8

JavidKhalil
JavidKhalil

Reputation: 59

Such a problem arise when you try to get branch your local git doesn`t know. First

git remote add origin yourremotegitrepository

After

git fetch //you should get all remote branches  

git checkout branchname

That is all.

Upvotes: 4

Tabish Zaman
Tabish Zaman

Reputation: 292

Three steps

  1. Write a command 'git fetch'
  2. then you will see the desired branch then switch to the relevant branch 'git checkout 'your_branch_name'
  3. then write a command 'git pull origin your_desired_branch_name'

Upvotes: 6

Matteus Barbosa
Matteus Barbosa

Reputation: 2725

check whether it is not a typo in the target file name. I was attempting to stage by typing

git add includes/connection..php

But I did not notice that I was using two dots But then I type

git add includes/connection.php

It works

Upvotes: 4

RS1980
RS1980

Reputation: 706

I got this error when trying to checkout a branch via:

git checkout branchX

which I had not checked out before. It only worked when explicitly stating the remote:

git checkout --track origin/branchX

The reason for this was, that I had 2 different remotes (origin + sth. else) configured in git config. As I didn't need the second remote, I removed it and voilá, it worked. The alternative to set the default remote via:

checkout.defaultRemote=origin

did not work for me

Upvotes: 17

Anshul
Anshul

Reputation: 425

I was getting the same error because I was checking out the branch that was not existing . So we need to make sure that the branch that we are checking out exists in the repository.

Upvotes: 1

Ankit Marothi
Ankit Marothi

Reputation: 1005

I faced the issue while switching my branch.

I did a git pull on the current branch and then tried to checkout the new one and it worked

git pull // on your old branch git checkout <new_branch>

Upvotes: 22

Korayem
Korayem

Reputation: 12497

I had a different root cause

I had a script that basically searches all branches matching jira issue key in the for "PRJ-1234" among all branches to execute a git branch checkout command on the matching branch

The problem in my case was 2 or more branches shared the same jira key and hence caused my script to fail with the aforementioned error

By deleting the old unused branch and making sure only a single branch had the jira key reference fixed the problem

Here's my code in case someone wants to use it

git remote update
git fetch --all --prune 
git branch -r --list *$1* | xargs git checkout --force

save this as switchbranch.sh

Then use it from the terminal ./switchbranch.sh PRJ-1234

Upvotes: 6

HarsH
HarsH

Reputation: 786

I fixed it by modifying my git config file Check your the config file in your git directory - .git\config

It previously had

[remote "origin"]
url = http://git.xyz.com/abc-group/pqr.git
fetch = +refs/heads/develop:refs/remotes/origin/develop

I fixed by modifying it to

[remote "origin"]
url = http://git.xyz.com/abc-group/pqr.git
fetch = +refs/heads/*:refs/remotes/origin/*

Notice the head was pointing to only one branch, so it couldnt find the reference to other existing branches, I changed it to * so it checks everything in origin.

Upvotes: 25

Mayank
Mayank

Reputation: 5569

I was getting this error when I tried to checkout new branch:

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

When I tried git checkout origin/<BRANCH-NAME>, I got the detached HEAD:

(detached from origin/)

Finally, I did the following to resolve the issue:

git remote update
git fetch 
git checkout --track origin/<BRANCH-NAME>

Upvotes: 491

AamirR
AamirR

Reputation: 12198

I copied remote origin url from another .git/config file, doing so my new .git/config file was missing following line in [remote "origin"] section

fetch = +refs/heads/*:refs/remotes/origin/*

Adding above line fixed error: pathspec 'master' did not match any file(s) known to git.

Upvotes: 10

Mohammad Faisal
Mohammad Faisal

Reputation: 5959

Alright, there are too many answers already. But in my case, I faced this issue while I was working on Eclipse and using git-bash to switch between branches/checkouts. Once I closed the eclipse and relaunched the git-bash to checkout branch everything worked well.

So my suggestion for you to double check if your repository is not being used by another application.

Upvotes: 1

Jeffrey Roosendaal
Jeffrey Roosendaal

Reputation: 7147

For me, it was a problem with my credentials


After trying some of the answer, one of them helped me to solve the problem:

Running git fetch threw the following error:

Could not resolve host: bitbucket.org

All I had to do was force my IDE (VS Code in my case) to remember my credentials:

git config --global credential.helper wincred

Git immediately synched all the changes, and git checkout <branche> works fine now!

Upvotes: 2

OpMt
OpMt

Reputation: 1742

git pull

That simply fixed it for me :)

Upvotes: 18

Shradhey Tripathi
Shradhey Tripathi

Reputation: 380

First, checkout parent branch.Then type

git fetch --all --prune 
git checkout <your branch>

Hope it helps!.

Upvotes: 9

Danimal
Danimal

Reputation: 1228

I had the same problem (with a version of git-recent) and discovered it was to do with color escape codes used by git. I wonder whether this could explain why this problem is occurring so commonly.

This is demonstrates what could be happening, though color is normally set in git configuration rather than the command line (otherwise it's effect would be obvious):

~/dev/trunk (master)$ git checkout `git branch -l  --color=always  | grep django-1.11`
error: pathspec 'django-1.11' did not match any file(s) known to git.
~/dev/trunk (master)$ git branch -l  --color=always  | grep django-1.11
  django-1.11
~/dev/trunk (master)$ git checkout `git branch -l  | grep django-1.11`
Switched to branch 'django-1.11'
Your branch is up-to-date with 'gerrit/django-1.11'.
~/dev/trunk (django-1.11)$ 

I figure a git config that doesn't play with the color settings should work color=auto should do the right thing. My particular issue was because the git recent I was using was defined as an alias with hard coded colors, and I was trying to build commands on top of that

Upvotes: -1

Related Questions