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

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

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  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" 

回答1:

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.



回答2:

I was getting following 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/ it went to detached HEAD

(detached from origin/)

Did following to resolve the issue

git remote update git fetch  git checkout --track origin/


回答3:

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.



回答4:

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:

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" 


回答6:

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.



回答7:

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

git fetch && git checkout 


回答8:

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



回答9:

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

Do a completed git clone will fix it.



回答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.



回答11:

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.



回答12:

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

Then git fetch

Now I am able to checkout branch by git checkout .



回答13:

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.



回答14:

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.



回答15:

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 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.



回答16:

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.



回答17:

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.



回答18:

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



回答19:

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.



回答20:

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

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 exactly file which caused trouble.

Afterwards I deleted the file from the repository with

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

and the problem was gone



回答21:

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.



回答22:

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.



回答23:

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.



回答24:

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.



回答25:

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 


回答26:

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

git commit -m "commit message in here" 


回答27:

$ 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).



回答28:

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.



回答29:

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.

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:

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.



回答30:

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!!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!