git rm --cached and fatal: pathspec
Asked Answered
S

5

28

I just tried to checkout my master branch and ran into:

error: Untracked working tree file 'app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate' would be overwritten by merge. 

So, I tried to delete this file from git (I'd already added an expression in .gitignore to catch it) using:

git rm --cached app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate

and got:

fatal: pathspec 'app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate' did not match any files 

So, at a bit of a loss. From my understanding the working file isn't the issue here. However, for completeness, a working file does exist. E.g.

ls -l app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate
-rw-r--r--  1 u  u  56061 24 Sep 12:42 app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate
Solipsism answered 24/9, 2012 at 12:13 Comment(4)
What makes you think that the working tree file isn't the issue? The error message says: "Untracked working tree file ... would be overwritten by merge". You're untracked working tree file is the issue; git doesn't want to blow it away as it contains untracked working changes.Deviationism
But how would the working tree file be overwritten if it isn't being tracked by git? E.g. git rm --cached says it did not match any file.Solipsism
It must be that a file exists at that location in the branch that you are trying to move to even though it doesn't exist in the commit that you are moving from. That's what the error message is telling you.Deviationism
Yes, thanks for that. Just posted an answer with the full details of how I solved it finally.Solipsism
S
19

So, the solution is this:

The file is untracked in this current branch B

But it exists in the branch we are trying to check out, branch A, so we get a warning that the file in our current working tree will be overwritten (even though we aren't tracking it)

So:

  1. delete the file in your existing directory (I just moved it somewhere out of the working tree initially to be safe) of branch B

  2. check out the branch you want - i.e. branch A

  3. Remove it from branch A using something like this:

    git rm --cached app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate

Note: Fwiw, Branch A was my master branch. Branch B was my dev branch.

Solipsism answered 24/9, 2012 at 12:46 Comment(0)
E
12

For the issue in the question title, you can generally solve it this way:

git rm --cached *

fatal: pathspec 'blah' did not match any files 

git ls-files

That will list the files git does have in its index, and you can then remove them explicitly one by one. If for example it lists img/blah.jpg:

git rm --cached img/blah.jpg

This will solve the pathspec error in the more general case, whether it's a branching issue as it was in the other answer here, or a new .gitignore entry, or a result of using 2 repos in the same dir, etc.

Escrow answered 2/1, 2015 at 19:42 Comment(0)
D
7

simple:

git add file.ext
git rm --cached file.ext

or

git add path/*
git rm --cached path/*
Deflower answered 3/10, 2017 at 22:23 Comment(2)
+1 for this, because it led to the truth about why I was getting this error. When I did git add some_file, the system replied The following paths are ignored by one of your .gitignore files: some_file. face palmSelfdetermination
After git add and git status, I get new file path/file1... Then after git rm --cached path -r I get Untracked files:(use "git add <file>..." to include in what will be committed) which I got before googling my problem - so nothing improved and a forever loop created.Brookes
L
2

Try --ignore-unmatch flag

I was getting the same error when trying to untrack some files that are already tracked by Git using a wildcard match, and since some seem not to exit, I was getting the error that file not matched, and that was crashing the whole operation. So I checked the manual and tried --ignore-unmatch flag, and it worked like a charm:

$ git rm --cached --ignore-unmatch cron/*.json

Lienlienhard answered 26/1, 2021 at 10:41 Comment(1)
Stays the same, no difference when using the flag.Brookes
R
0

If your files are coming from previous commits, you need to remove all the files from the previous commits.

git filter-branch --force --index-filter \
  'git rm -r --cached --ignore-unmatch PATH_TO_FILES_TO_REMOVE' \
  --prune-empty --tag-name-filter cat -- --all
Rhachis answered 30/6, 2022 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.