1109

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"
23
  • Might this help? stackoverflow.com/questions/2527355/…
    – Boldewyn
    May 13, 2011 at 9:10
  • Does it work if you do: git checkout refs/heads/user_controlled_site_layouts? May 13, 2011 at 9:11
  • 2
    Mark - nope, still get the same error. May 13, 2011 at 9:18
  • 1
    Seeing your update, I'm not sure how you've got a branch that appears in git branch -a but not in git show-ref. Does the file .git/refs/heads/feature/user_controlled_site_layout actually exist? If so, what does cat .git/refs/heads/feature/user_controlled_site_layout give? May 13, 2011 at 9:18
  • 1
    I got the same error, I created the branch from another repo and try to checkout that branch to another repo. So I got it because of the different repos. Aug 8, 2019 at 7:18

59 Answers 59

1517

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.

12
  • 42
    For further clarification, git fetch is useful when you need to synchronize your local repository with the remote repository, but you don't want to merge the changes into your working folder. Dec 13, 2013 at 19:19
  • 172
    In case of a clone with multiple remotes, the git checkout Branch does not work. With multiple remotes just the Branch name is ambiguous and requires the specification of remote/branch. The command git checkout -b branch remote/branch works in that scenario. Jul 15, 2014 at 0:57
  • 10
    @ Aleks it's not the accepted answer because it has nothing to do with the OP's question. He could no longer check out a branch he'd previously checked out (ie created) locally. Just because other people with a different, much more basic problem have found & upvoted this answer (which is completely trivial and well-known to any git user with more than two days experience) doesn't mean the OP should accept it. Jun 15, 2016 at 19:11
  • 28
    git fetch could do the job. But it may fail to get all the branches from remote. You'd need to set the fetch match pattern. git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" refer: stackoverflow.com/questions/11623862/…
    – Jichao
    Dec 30, 2016 at 8:27
  • 5
    git fetch is a bizarre and crazy thing, and it takes more than 2 days to learn about these crazy little tarpits.
    – Warren P
    Jan 9, 2018 at 22:51
484

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>
11
  • 17
    with this "checkout" command I have this message: fatal: Cannot update paths and switch to branch 'foo' at the same time.
    – NEOline
    Nov 12, 2015 at 11:27
  • 2
    After manually modifying the remote address, I could not checkout new branches. "git remote update" seemed to do the trick.
    – martin
    Jan 29, 2016 at 10:04
  • 2
    For me git fetch alone didn't work, but this solution (that includes remote update and track origin works perfectly.
    – artm
    Oct 18, 2019 at 21:53
  • 1
    Having tried all other solutions, none of them worked for me. Finally this one works for me. Thank you!! I think the key is to do git remote update
    – Hui Zheng
    Aug 30, 2022 at 0:37
  • 1
    git remote update worked for me too. But do we know why we have to do that? Isn't git fetch supposed to work (has worked on all of my other repos). Sep 7, 2022 at 6:58
173

I got this error for a branch that was remote and had no local tracking branch. Even though I'm certain I've checked out remote branches via a simple

git checkout feature/foo

in the past, to get around this error I had to

git checkout -t -b feature/foo origin/feature/foo

I have no idea what I did to get myself into that situation either.

7
  • 2
    Unfortunately, I didn't push it to the remote git server. May 31, 2011 at 4:06
  • This worked for me. It happened on git v 1.6 on FC10 machine. Feb 14, 2013 at 22:17
  • 31
    Git usually guesses the remote branch to check out, but when using more than one remote, it seems like it no longer can do that. Source: makandracards.com/makandra/… Sep 24, 2013 at 15:07
  • 1
    doing git checkout feature/foo instead of just git checkout foo worked for me Apr 14, 2014 at 15:01
  • 1
    Doesn't passing `-b- create a new branch? Does it create a new local branch which tracks the remote branch, or could this command create a new remote branch? Jun 21 at 20:19
137

If you deleted a branch with git branch -D yourbranchname and pulled/cloned again your repo, you may need to create your local branch again.

Try:

git checkout -b yourbranchname
5
  • this option always works, even where is files already changed and u wish to push them to newly created branch.
    – Nerius Jok
    May 15, 2018 at 7:50
  • This was a helpful comment because while I shared the same problem/question as the OP here, the answers provided assumed a straightforward situation. Unfortunately, in my situation, I had previously created ONLY a local branch, then deleted it as noted here by @Francisco Alvarez, so no matter how I tried the other solutions here, I could not pull the new remote branch. This answer saved my bacon.
    – David
    Sep 20, 2019 at 13:57
  • this finally worked for me with un-editable jenkins configurations, thanks! (they are autogenerated by other DSL) Aug 16, 2021 at 16:25
  • Thanks a lot. i have been trying all other ways.. but this really worked.
    – Dip
    Nov 3, 2022 at 2:25
  • This way very simple and it worked for me.
    – baohoang
    Dec 25, 2022 at 8:00
130

I have the same questions, and got some information from this link: git fetch doesn't fetch all branches

So now, I may not sure how this situation happened, at least we can solve it:

Step 1. Check your "remote.origin.fetch" setting, should be like this

$ git config --get remote.origin.fetch

+refs/heads/private_dev_branch:refs/remotes/origin/private_dev_branch

Step 2. Change "remote.origin.fetch" to fetch everything

$ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

$ git config --get remote.origin.fetch

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

Then, you can try "git pull" (maybe "git fetch origin" also works but I didn't try) to get all the branch.

6
  • @onionjake is right, sorry that I didn't check the questions in every details. Just I got the same error messages while trying the same action of "git checkout <existed_branch>", but different from the originator's problem actually----- originator could see the branch locally while I cannot. My answer may solve the problem for whom didn't fetch all branches in advanced. But not the situation for originator's problem.
    – bearzyj
    Aug 8, 2017 at 6:24
  • 9
    Many people including me receive this error, because they could clone a repo with --branch flag, therefore, even after git fetch they don't get other branches and can't checkout anything from remote. This fixes this issue. Thanks! Jan 27, 2019 at 19:37
  • Thank you very much. I was using tensorflow's devel docker image, and it restricts the branch to certain version. After setting up fetch config, now I can checkout. Oct 30, 2019 at 3:51
  • Tks, solution my problem. Nov 4, 2020 at 14:46
  • Thanks! 'git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"' just saved my life! Jan 15, 2021 at 10:17
46

I had the same issue for one of my branch.

These commands work for me.

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

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>

3
  • 3
    Interesting solution, but it's got a weakness. I just tested it, and while it creates the local branch as desired, it doesn't set the upstream on it. For this to work as one would expect, you'd have to run git branch -u origin/<branchName> after doing your checkout.
    – joanis
    Oct 27, 2021 at 12:48
  • I had to set the upstream by running git push --set-upstream origin <branchName> in order to be able to push changes.
    – Emi
    Apr 1, 2022 at 8:32
  • git pull origin <branchName> worked for me. Jul 21, 2022 at 7:24
29

Git Windows users beware - without the --icase-pathspecs or GIT_ICASE_PATHSPECS = 1 env var setting, that git pathspecs will be case-sensitive, in which case

git checkout origin/FooBranch "Some/Path/To/File.txt"

is not the same as

git checkout origin/FooBranch "some/path/to/file.Txt"
1
  • 1
    One thing to note that wasn't clear from documentation is that the --icase-pathspecs parameter needs to come first or at least before -C <path>
    – sonyisda1
    Aug 22, 2018 at 15:18
24

If branch name and you dont have any uncommited file, then try this

git fetch && git checkout <branch name>
2
  • This doesn't appear to be related to the question at all?
    – onionjake
    Aug 8, 2017 at 4:54
  • I got the same issue mentioned above, resolved using following commands Aug 8, 2017 at 12:28
23

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.

3
  • 2
    Thanks, buddy, hours of efforts and finally I landed with your solution. Apr 5, 2020 at 7:51
  • this should be marked as correct answer
    – Godwin
    Feb 14, 2022 at 20:03
  • Thanks, only answer that helped me too. You can actually do: git config --local remote.origin '+refs/heads/*:refs/remotes/origin/*'
    – sripwoud
    Nov 14, 2022 at 17:33
22

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>

3
  • Really useful:)
    – vikifor
    Mar 26, 2020 at 15:22
  • Why would this work? Does git pull do more than pull the current branch? Jun 3, 2020 at 17:32
  • This might just be a case that you are trying to pull from a branch created by someone else and that's not yet pulled in your current repo. Jun 4, 2020 at 7:23
18
git pull

That simply fixed it for me :)

1
  • The reason WHY this worked for me is that I wanted to git checkout master path/to/some/file.ext, but my LOCAL master wasn't in sync with remote master because I had been on a development branch for a long time.
    – Chuck
    Apr 20, 2022 at 15:32
17

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

2
  • 2
    I added this answer because all the other suggested answers did not work for me, so I hope this helps someone who has the same cause (2 remotes).
    – RS1980
    Nov 21, 2019 at 11:22
  • This worked for me after I accidentally ran git rm -f .git/index.lock and then correctly ran rm -f .git/index.lock because I was getting fatal: Unable to create '/home/sagemaker-user/qubed2/.git/index.lock': File exists. from git gui shenanigans
    – DataJack
    Feb 2, 2022 at 17:32
16

I got the same problem because I used git clone --depth=1, which implies --single-branch.

Do a completed git clone will fix it.

2
  • Thanks for pointing it out. It is exactly the same issue I was facing. Thanks Feb 2, 2017 at 7:01
  • A complete clone might not be required. If the branch is created by someone else AFTER you originally cloned the repo, you get this error because your local repo does not have any information about this new branch. Just switch to master and do a git pull. Then try to checkout the new branch.
    – Yasin
    Aug 2, 2017 at 18:12
13

I got this when I did the following:

  • Used IntelliJ IDE, connected to git
  • Created a new file, and added to git
  • Renamed the new file

When I tried to check in the directory, I got this error.

To fix:

I opened the repo in git extensions. I saw that the file (with the old name) was staged. But since it didnt exist anymore, it could not be committed.

I simply unstaged this file.

Then I re-added the file (this time correctly named) into git and committed without errors.

2
  • The funny thing is, it still works with TortoiseGit even if it does not in intellij Mar 25, 2015 at 12:01
  • 3
    This helped me alot. I used SourceTree to find that f*ckr of a file and unstaged it.
    – Vinchenzo
    Jun 18, 2015 at 8:09
11

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
10

I had this problem today I was trying to git checkout foo and got error: pathspec 'foo' did not match any file(s) known to git.

It turns out I was in the wrong repo. So lesson learned: check which repo you're looking at before freaking out.

3
  • Ha - yes this was me - created branch in Bitbucket from Jira ticket, didn't notice it created it in some random repo
    – Keir
    Sep 20, 2017 at 9:29
  • sidenote: I was working with multiple remotes, had one set properly, and the other pointing to the wrong repo. sooo. thanks for the reminder, I could easily see my issue when I did a git remote -v
    – Dreamling
    Jun 20, 2018 at 17:26
  • This was my problem too. We had moved the location of the repo in gitlab, but I was still working from a previous checkout of it before the move.
    – senorsmile
    Jan 31, 2022 at 23:03
10

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.

0
9

First, checkout parent branch.Then type

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

Hope it helps!.

1
  • This works because of the --all -- I was receiving the error because I had 2 remotes, and one had fetched the new branch while the other hadn't. Either remove the second remote, or do git fetch --all
    – jessica
    Jun 2, 2022 at 17:53
8

I encountered this same issue when I was first playing around with git. When attempting my first commit...

git commit -m 'first commit!'

I got the error mentioned by the OP...

error: pathspec 'commit!'' did not match any file(s) known to git.

I thought I might have been confusing git by using a keyword in the commit message, so I tried a few other words and received the same error.

Finally I used double-quotes in the message...

git commit -m "first commit!"

This turned out to be successful...

[master (root commit) 0000000] first commit!
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dummyDoc.txt
8

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.

1
  • I wonder why this worked? I initialized git locally, created a branch right away, added and commited files, then pushed it. Then tried to go to checkout to main - got the error: pathspec 'main' did not match any file(s) known to git . Ran this git fetch -p - it worked. I wonder if this is because it got updated remote references to main which it did not have before? Or likely when I initialized the repo with git init, it created a master branch - but when I created a repo on github there - its branch is main. Dec 17, 2020 at 3:26
7

On Windows OS by default git is instaled with

core.ignorecase = true

This means that git repo files will be case insensitive, to change this you need to execute:

\yourLocalRepo> git config core.ignorecase false

you can find this configuration on .git\config file

0
7

I had made a silly mistake of not providing -m flag while committing (lol happens)

git commit -m "commit message in here"
1
  • same situation here. just forget "-m" get the same error msg.
    – Niuya
    Aug 14, 2018 at 3:19
6

I had the same issue.. I thought I had branch named foo when I try to:

git checkout foo

I was getting:

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

Then I tried the full branch name:

git checkout feature/foo

then worked for me.

6

In my case I have TWO branch 1) master(which is for live server) 2) dev(test server). I had set multiple remote to push code on respective server. When I tried to switch branch I got the error like error: pathspec 'master' did not match any file(s) known to git.

You can see them by git remote -v. I had removed other remote except origin remote by git remote remove <remote-name>

Then git fetch

Now I am able to checkout branch by git checkout <branch-name>.

6

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'
5

If it happens on Windows, it is probably the filename case issue.

I had this error today - I've created new file, added to GIT, then I changed one letter in filename from lower to upper and then I couldn't to anything - commit, revert, delete file from repo.

The only solution I found was changing the filename again back to exact same case when I added this file to GIT, then doing GIT revert to remove this file from GIT, then changing filename again as I want. After those changes I could commit to repo and then push without any problem.

1
  • This helped, but it should be mentioned to do the renaming via git mv. Guess you've done it this way, because doing it via my IDE (IntelliJ PhpStorm) failed for me...
    – Dennis98
    Dec 20, 2015 at 3:44
5

Happened to me after renaming an uncommitted file in Android Studio.

Git seemed to have the old version in its repository, even if it didn´t exist anymore.

fetch, pull, checkout, add all and so on did not help in my case!

So I opened the Git GUI of TortoiseGit which showed me the exact file that caused trouble.

Afterwards I deleted the file from the repository with

git rm -r --cached /path/to/affected/file

and the problem was gone

5

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

1
  • For me, it was also the right upstream path/name and including to fetch all of the remote tags as well "git fetch --all --tags --prune" finding the right name: "git branch -a | grep some_upstream" before checking it out with the full path as "git checkout -f --track -b new_branch remotes/upstream/some_upstream_branch"
    – kisna
    May 1, 2020 at 0:02
5

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.