1090

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 <xxx@xxxxx.xxx>
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"
Community
  • 1
  • 1
Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69
  • Might this help? http://stackoverflow.com/questions/2527355/using-the-slash-character-in-git-branch-name – Boldewyn May 13 '11 at 09:10
  • Does it work if you do: `git checkout refs/heads/user_controlled_site_layouts`? – Mark Longair May 13 '11 at 09:11
  • Boldewyn - thanks, but it didn't help. But I've edited the question to show what's in the `.git/refs` folder. – Ramon Tayag May 13 '11 at 09:17
  • 2
    Mark - nope, still get the same error. – Ramon Tayag May 13 '11 at 09: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? – Mark Longair May 13 '11 at 09:18
  • Mark, I've updated it with the contents of `.git/refs/heads/feature/user_controlled_site_layouts`. – Ramon Tayag May 13 '11 at 09:22
  • Did you try `git checkout feature/user_controlled_site_layouts --`? (You might want to avoid naming branches with slashes in them - hyphens are perhaps a good replacement.) – Cascabel May 13 '11 at 12:15
  • Jefromi, I first started using slashes when I implemented git-flow (http://jeffkreeftmeijer.com/2010/why-arent-you-using-git-flow/). It doesn't seem to be a problem for them. I also read that slashes aren't a problem (don't remember where). Here's the response to the command you pasted: `fatal: invalid reference: feature/user_controlled_site_layouts`. – Ramon Tayag May 15 '11 at 06:05
  • Branch names with slashes are fine. It is suspicious that `git branch -a` shows your problematic branch, yet `git show-ref` does not. I can induce this problem by introducing an “invisible” character (I used NUL, ASCII 0) anywhere in the first 40 bytes of a loose ref file. The contents of the file look fine with `cat`, it shows up in `git branch -a`, but the branch is dropped from `git show-ref` (and `git branch -av` gives an error: `error: branch 'whatever' does not point at a commit`). Try `od -a .git/refs/heads/feature/user_controlled_site_layouts`. Anything besides 0-9a-f + NL? – Chris Johnsen May 31 '11 at 06:38
  • Unfortunately I no longer have the git repo available. After a while of not being able to solve this, I think I just ended up forgoing the work in that branch. The solution would not have fixed mine anyway (I think) -- I never pushed the branch to a remote repo. – Ramon Tayag Oct 23 '12 at 13:53
  • I'm getting this same error. The only thing I can think of is that I'm on a Lustre parallel filesystem. I've had things behave oddly before, usually dealing with unsupported file locking features. Are you on Lustre? – Scott Feb 08 '13 at 18:57
  • I was not on Lustre. I was on Ubuntu or OSX -- don't remember which. – Ramon Tayag Nov 19 '14 at 22:34
  • 30 Solutions collect form web for “Git: cannot checkout branch – error: pathspec '…' did not match any file(s) known to git” http://gitbaby.com/git-cannot-checkout-branch-error-pathspec-did-not-match-any-files-known-to-git.html – Bipon Biswas Dec 06 '17 at 10:41
  • Make sure you're in the correct directory relative to the file you want to checkout. – Will Aug 23 '18 at 19:16
  • 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. – Kushan Gunasekera Aug 08 '19 at 07:18
  • Seems that this issue is in git bash only; **It's working fine in SourceTree**. – Tushar Walzade Oct 22 '19 at 07:08
  • The most important check is that you are pointing to the correct repo. I got lots of complain about this error and it turns out that the user was pointing to REPOB when is suppose to point to REPOA. The Remote repo that was standarized was REPOA. first smoke test to be sure... – Andre Leon Rangel May 29 '20 at 04:37
  • As of [October 1, 2020](https://www.zdnet.com/article/github-to-replace-master-with-main-starting-next-month/#:~:text=GitHub%20repositories%20are%20where%20users,of%20a%20source%20code%20repository.&text=1%2C%202020%2C%20any%20new%20repositories,master%2C%22%20the%20company%20said.), GitHub changed the name of the master branch to "main" to avoid negative connotations. `git checkout main` should work. – Raphael Oct 19 '20 at 06:45
  • for < 0.001 % try default cloning and then checking out whatever branch. It wasn't working for me when i had cloned a specific branch and had added multiple remote. – Abhishek Kumar Dec 09 '20 at 10:12
  • It happened to me when my working directory was a submodule! – Mahdiyeh Dec 18 '20 at 09:35
  • It happened to me in windows, running a for loop under git bash. The branch name had a \r\n with the \r causing the issue. once i changed the EOL to just \n, it worked fine – codester Jan 14 '21 at 18:13
  • FYI looks like github changed the name of the primary branch from 'master' to 'main' recently (2021) – wordsforthewise Jan 24 '21 at 19:42
  • `git pull origin ` works for me – Timoth Dev A Jul 21 '22 at 07:26

59 Answers59

1500

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
MarkoHiel
  • 15,481
  • 2
  • 21
  • 29
  • 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. – Mark Lakata Dec 13 '13 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. – Umair Ishaq Jul 15 '14 at 00:57
  • 1
    I had managed to create a local "master" somehow, so although "git fetch" got me closer, I had to delete my local master first ("git branch -d master"), before switching to the remote master ("git checkout master"). In case that helps anyone else who is as silly as me! – Jamie Brown Oct 23 '14 at 00:12
  • There is also a command like `git remote add origin ssh://...` which you can use if your local master branch is not tracking the remote one. – MarkoHiel Oct 23 '14 at 09:30
  • Some clarifications on @UmairIshaq comment, the reason `git checkout Branch` does not work is only if that branch exists in the clones. `git` is smart enough to still checkout a branch from a clone as long as that branch only is not cloned anywhere else. Ex. branch: 'site_layout' lives on remote 'example' and nowhere else, `git checkout site_layout` will produce a local branch that is tracking on `example/site_layout` – Sam Tsai Nov 14 '14 at 20:11
  • 9
    @ 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. – Michael Johnston Jun 15 '16 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: http://stackoverflow.com/questions/11623862/git-fetch-doesnt-fetch-all-branches – Jichao Dec 30 '16 at 08: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 09 '18 at 22:51
  • I have got this error on windows when I set commit message in '' not "" for an example `git commit -m "Marats version"` works fine, but `git commit -m 'Marats version'` isnt – Marat Mkhitaryan Mar 02 '19 at 13:59
  • Maybe you dont have a branch so you encountered this problem. you can check are there this branch ? and then you write > git checkout -b branchname – MahmutKarali Nov 11 '19 at 06:17
  • For me, git reset . worked – Aloha Churchill Dec 07 '21 at 02:14
  • doesn't work... – Ozan Kurt Dec 14 '21 at 05:07
  • `git pull origin ` worked for me. – Timoth Dev A Jul 21 '22 at 07:25
  • git fetch command is helpful to me – lam vu Nguyen Dec 22 '22 at 17:12
  • Oh my god it's working.....Thank you. – Naresh Aug 22 '23 at 12:31
476

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>
Kon
  • 4,023
  • 4
  • 24
  • 38
Mayank
  • 5,411
  • 1
  • 16
  • 16
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.

Gregory McIntyre
  • 2,051
  • 1
  • 12
  • 7
  • 2
    Unfortunately, I didn't push it to the remote git server. – Ramon Tayag May 31 '11 at 04:06
  • This worked for me. It happened on git v 1.6 on FC10 machine. – FractalSpace Feb 14 '13 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: http://makandracards.com/makandra/14323-git-how-to-check-out-branches-that-exist-on-multiple-remotes – Elijah Lynn Sep 24 '13 at 15:07
  • 1
    doing git checkout feature/foo instead of just git checkout foo worked for me – Alejandro Moreno Apr 14 '14 at 15:01
  • this started happening to me (in a repo with many remotes and hundreds of branches from which I'd been successfully checking out branches from origin for 3 years) and I was mystified. Upon inspection, I discovered I'd added a new remote and I'd done it in with copy/paste in a text editor instead of using the git command, and forgot to change the `fetch = +refs/heads/*:refs/remotes/origin/*` line to match the new remote name. LOL. Maybe this happened to makandra? because after fixing it, checking out branch once again gets it from origin, even though I have many remotes. – Michael Johnston Jun 15 '16 at 19:23
  • 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? – SendETHToThisAddress Jun 21 '23 at 20:19
  • Yes it creates a new *local* branch that mirrors/tracks the existing remote branch. – Gregory McIntyre Jun 22 '23 at 23:21
136

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
Warren P
  • 65,725
  • 40
  • 181
  • 316
Francisco Alvarez
  • 1,571
  • 1
  • 9
  • 10
  • this option always works, even where is files already changed and u wish to push them to newly created branch. – Nerius Jok May 15 '18 at 07: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 '19 at 13:57
  • this finally worked for me with un-editable jenkins configurations, thanks! (they are autogenerated by other DSL) – Max Cascone Aug 16 '21 at 16:25
  • Thanks a lot. i have been trying all other ways.. but this really worked. – Dip Nov 03 '22 at 02:25
  • This way very simple and it worked for me. – baohoang Dec 25 '22 at 08:00
126

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.

Community
  • 1
  • 1
bearzyj
  • 1,521
  • 1
  • 9
  • 10
  • @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 ", 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 08 '17 at 06: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! – Orif Khodjaev Jan 27 '19 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. – Michael_Zhang Oct 30 '19 at 03:51
  • Tks, solution my problem. – Marquin Ferreira Nov 04 '20 at 14:46
  • Thanks! 'git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"' just saved my life! – Charles Tempo Jan 15 '21 at 10:17
  • Upvoting this since it helped me (esp since I forgot that I had cloned branch at first). And this is the second or third link on google for the error message that git gives. – tpb261 May 24 '21 at 10:40
46

I had the same issue for one of my branch.

These commands work for me.

git fetch --all
git checkout <branch-name>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Pranay kumar
  • 1,983
  • 4
  • 22
  • 51
38

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>

Botnetdobbs
  • 536
  • 4
  • 5
  • 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/` after doing your checkout. – joanis Oct 27 '21 at 12:48
  • I had to set the upstream by running `git push --set-upstream origin ` in order to be able to push changes. – Emi Apr 01 '22 at 08:32
  • `git pull origin ` worked for me. – Timoth Dev A Jul 21 '22 at 07: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"
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • 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 ` – sonyisda1 Aug 22 '18 at 15:18
24

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

git fetch && git checkout <branch name>
Sajin M Aboobakkar
  • 4,051
  • 4
  • 30
  • 39
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.

HarsH
  • 760
  • 3
  • 12
  • 27
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>

Ankit Marothi
  • 955
  • 10
  • 14
18
git pull

That simply fixed it for me :)

OpMt
  • 1,723
  • 4
  • 19
  • 24
  • 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 '22 at 15:32
18

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

RS1980
  • 634
  • 8
  • 13
  • 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 '19 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 02 '22 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.

Huachao Huang
  • 216
  • 2
  • 5
  • Thanks for pointing it out. It is exactly the same issue I was facing. Thanks – Mukesh Kumar Feb 02 '17 at 07: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 02 '17 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.

Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44
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.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
James Harrington
  • 3,138
  • 30
  • 32
  • 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 '17 at 09: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 '18 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 '22 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.

AamirR
  • 11,672
  • 4
  • 59
  • 73
10

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
BollMose
  • 3,002
  • 4
  • 32
  • 41
9

First, checkout parent branch.Then type

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

Hope it helps!.

  • 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 02 '22 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
TheLastGIS
  • 426
  • 5
  • 18
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pramod KP
  • 188
  • 2
  • 8
  • 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. – Alexey Shevelyov Dec 17 '20 at 03: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

Vasile Bors
  • 656
  • 6
  • 14
7

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

git commit -m "commit message in here"
Ajain Vivek
  • 1,111
  • 1
  • 12
  • 20
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.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
tokhi
  • 21,044
  • 23
  • 95
  • 105
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>.

NiRmaL
  • 2,866
  • 1
  • 18
  • 15
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'
Tabish Zaman
  • 227
  • 3
  • 4
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.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 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 '15 at 03: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

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
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

Korayem
  • 12,108
  • 5
  • 69
  • 56
  • 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 01 '20 at 00: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

Matteus Barbosa
  • 2,409
  • 20
  • 21
5

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 
Niroshan Ratnayake
  • 3,433
  • 3
  • 20
  • 18
4

In my case I had renamed a file changing the case of the file, i.e. SomeFile.js -> someFile.js

I think that was related to the problem. Doing a git fetch didn't fix the issue.

I moved the files out of my project, did a fetch, and did a push without them. Then I did a fetch, added them back, and did a push, and it worked. I don't know if all those steps were needed, but it did ultimately work.

Kip
  • 560
  • 7
  • 16
4

None of these answers solved my issue:

Nikolai@CALIGARI-7 ~/Documents/NetBeansProjects/Version (master)
$ git log --format=oneline
b9cc6a9078312865280fb5432a43e17eff03a5c6 Formatted README
288772f36befe6bd60dd41b8185f1e24e0119668 Updated README documentation
d2bdbe18f4169358d46fad50eacfb89786df3bf8 Version object v3.0.0-SNAPSHOT
a46b1910a3f548b4fa254a6055d25f68d3f217dd VersionFactory is now Platform agnostic
24179ae569ec7bd28311389c0a7a85ea7b4f9594 Added internal.Platform abstraction
252b684417cf4edd71aed43a15da2c8a59c629a7 Added IPlugin implementation for Sponge
e3f8d21d6cf61ee4fc806791689c984c149b45e3 Added IPlugin implementation for Bukkit
aeb403914310b4b10dee9e980cf64472e2bfda79 Refactored Version.java
ef50efcff700c6438d57f70fac30846de2747a7e Refactored TesterFactory
a20808065878d4d28657ae362235c837cfa8e625 Added IPlugin abstraction
9712a3575a70060d7ecea8b62bb5e888fdc32d07 Heavily refactored Tester
02d025788ae740dbfe3ef76a132cea8ca4e47467 Added generic Predicate<T> interface
9c565777abea9be6767dfdab4ab94ed1173750dd Minor refactoring of testCompareTo()
2ff2a28c221681e256dcff28770782736d3a796a Version object v2.0.1
d4b2e2bd830f77cdbc2297112c2e46b6555d4393 Fix compareTo()
05fe7e012b07d1a5b8de29804f96d9a6b24229a1 Make compareTo() fail
6e85371414357a41c1fc0cec0e75adba92f96832 Fix VersionFactory passing null
c1fd1f032f87d860d5ed9d6f6679c9fa522cff8d Version object v2.0
62c3a92c008a2ed11f0a4d016080afc3541d0700 Version object v1.2
c42e9e617128085e872c51b4d977a04e48d69e8f Deprecated, doc'd, future-proofed getNm


Nikolai@CALIGARI-7 ~/Documents/NetBeansProjects/Version (master)
$ git checkout 3a796a
error: pathspec '3a796a' did not match any file(s) known to git.

I was trying to go back and build the commit for Version object v2.0.1. Luckily, I got the idea to try the whole hash code and it worked ! Which means that I was using the wrong end of the hash code.

Nikolai@CALIGARI-7 ~/Documents/NetBeansProjects/Version (master)
$ git checkout 2ff2a
Note: checking out '2ff2a'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 2ff2a28... Version object v2.0.1

Nikolai@CALIGARI-7 ~/Documents/NetBeansProjects/Version ((2ff2a28...))
$

As shown above, for partial hash codes, you must supply the front-end, not the back-end.

Nikolaii99
  • 115
  • 6
4

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.

3

in my case I enter submodule directory without doing

  • git submodule init
  • git submodule update

So git was linked to the parent folder that indeed missed that branch.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
3

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

qucumbah
  • 73
  • 4
2

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!

Jeffrey Roosendaal
  • 6,872
  • 8
  • 37
  • 55
2
$ cat .git/refs/heads/feature/user_controlled_site_layouts
3af84fcf1508c44013844dcd0998a14e61455034

Can you confirm that the following works:

$ git show 3af84fcf1508c44013844dcd0998a14e61455034

It could be the case that someone has rewritten the history and that this commit no longer exists (for whatever reason really).

rtn
  • 127,556
  • 20
  • 111
  • 121
  • How could the commit no longer exist if it is referenced by that branch? – svick May 13 '11 at 20:10
  • Magnus, I added the output of what you asked to my question. – Ramon Tayag May 15 '11 at 06:09
  • This is indeed a weird problem. Does "git show-ref --verify refs/heads/feature/user_controlled_site_layout" give you any more information as to what may be the problem? – rtn May 15 '11 at 11:13
  • I just get this `fatal: 'refs/heads/feature/user_controlled_site_layout' - not a valid ref` – Ramon Tayag May 17 '11 at 09:07
1

I was getting the same pathspec error on git-bash. I used Tortoise git on windows to switch/checkout the branch.

captain
  • 815
  • 14
  • 26
1

This can be caused when upstream rewrites history.

When this happens, I toss all affected repos, clone them fresh from upstream, and use 'git format-patch' / 'git am' to ferry any work in progress from old world to new.

Dan Kegel
  • 559
  • 5
  • 11
1

I faced a similar issue. What led me into this trouble was: I had multiple remote branches and I deleted the folder that had the code. I did a get pull. :

git pull git@git.corp......

Then I added remote repository :

git remote add upstream git@git.corp.......

Then I tried to change branch to some other branch say AAAA and I got error

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

After spending an hour I found a solution. I deleted the source folder again. Then I did a git pull :

git pull git@git.corp......

Then I changed branch before linking it to remote repository :

git checkout AAA

then I added it to remote repository

git remote add upstream git@git.corp......

After this I can easily switch branches. Although this is not a standard way but it worked for me after I tried all of above options.

Bram
  • 2,515
  • 6
  • 36
  • 58
Amit Kumar
  • 1,780
  • 1
  • 10
  • 8
1

I had this problem when working with Git on Windows. In my case, it was a case issue. I had already added and committed a file to my repository and later changed only its case. To solve the problem, I renamed the file to match the original case and rename it again with the git mv command. Appearently, this lets git track the rename.

Note: I was using Cygwin.

Thomas Eizinger
  • 1,404
  • 14
  • 25
1

I faced this problem last time, and the thing I have done is remove the folder (or source code) related to that branch.

Ex: the branch origin/foo create folder foo at your local, so I just delete it and use git fetch $ checkout to get code back from remote.

Or you can do it by create folder at local same as the branch you intended to checkout.

Ex: create folder foo at local, after that, use Git command: git branch --set-upstream-to=origin/foo foo, that all. Use git pull to get code.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
blackberry
  • 19
  • 1
1

I my case I run "git fetch" before "git branch ...:, but obtained the same error. Then I analyzed it and found error in my branch name. Fixed it and switch to branch successfully.

Viktor Chmel
  • 145
  • 7
1

One possible reason, If you are working on windows machine with MINGW*

ISSUE

Check the branch name you want to checkout.Windows omits character ' from branch name while parsing command.

for example : if you have a branch name like bugfix/some-'branch'-name

When you issue command : > git checkout bugfix/some-'branch'-name it will parse it as bugfix/some-branch-name, notice it omits ' character.

REMEDY

To checkout this kind of branch name, add escape character while issuing command.

Type : > git checkout bugfix/some-\'branch\'-name

It shoud work then!!

Piyush Sagar
  • 2,931
  • 23
  • 26
1

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.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
1

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.

Anshul
  • 415
  • 4
  • 15
1

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.

Pedro Coelho
  • 1,411
  • 3
  • 18
  • 31
1

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 git@bitbucket.org:org_name/project_name.git
Fasil kk
  • 2,147
  • 17
  • 26
1

This problem is caused when doing a commit without '-m'.

Wrong:

git commit "..."

Correct:

git commit -m "..."
aloser01
  • 49
  • 4
0

I got this in Github desk top after clicking "Update from..." when the wrong repo was selected. I then changed repo to the correct one but when I tried to remove changes I got this error. That's because these were new files in the repo I errantly selected but not in the one I wanted to update from.

I simply changed the repo selector back to the one I incorrectly selected the first time then I was able to remove the changes.

Then I changed the repo selector to the one I wanted.

user2568374
  • 1,164
  • 4
  • 11
  • 21
0

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:

git@github.com:user/reponame?ref=1.0.0

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

git@github.com:user/reponame?ref=v1.0.0

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

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Putnik
  • 5,925
  • 7
  • 38
  • 58
-1

This happened to me as well on Windows on two local branches I created myself. Once out of the branch, I was not able to checkout

bugfix_#303_multiline_opmerkingen_not_displaying_correctly_in_viewer

nor

bugfix_339_hidden_fields_still_validated

you can see in the screenshot from Git bash below.

enter image description here

When I tried using Git GUI, I wasn't even able to see the branches at all. However when I used Pycharms git tool, I saw this:

enter image description here

So for some reason windows decided to add some indecipherable character to my branch names and that is the reason it didn't work. Renaming the branches in Pycharm did the trick, so guess this would work on other IDEs as well.

tarikki
  • 2,166
  • 2
  • 22
  • 32
-1

Here is how I solved my error!

First I deleted my repo locally and also from my Github. Then I forked and cloned to my local repository.

After that, I made my change to it and finally

Created My branch using

$git checkout -b moh_branch

change working branch

$git checkout moh_branch

push the branch to GitHub

$git push origin moh_branch

After that, I am able to commit my change as follow:-

$git add .

$git commit -m "updated readme.md"

$git push origin moh_branch

Succeeded 100% for me!!

Moh K
  • 484
  • 1
  • 4
  • 17
-1

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

Danimal
  • 1,208
  • 10
  • 21
-2

Since your feature branch exists in remote, it's your local repo that's corrupted. So, delete your local repo and re-clone. Now 'git checkout <branch_name>' should work for you.

j.a.estevan
  • 3,057
  • 18
  • 32
Ninos
  • 224
  • 4
  • 18
  • Deleting the repo is bad advice. The branch did not exist on the remote and he would have lost all his work on the branch. – onionjake Aug 08 '17 at 04:55
-3

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.

Kate
  • 1,107
  • 8
  • 7
-4

This worked for me. It discards all local changes and resets it to the last commit.

git reset --hard
Tim Siwula
  • 966
  • 11
  • 15
-4

If you're on Windows you just probably change filename to lower/upper case like File.txt - file.txt

So check what file you have in git and rename it to what you want:

git status

Then I need file.txt -> but git status gaves me File.txt so just rename it

git mv File.txt file.txt

And problem is solved.

Manic Depression
  • 1,000
  • 2
  • 16
  • 34