Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rm --cached and fatal: pathspec

Tags:

git

git-rm

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 
like image 809
Snowcrash Avatar asked Sep 24 '12 12:09

Snowcrash


People also ask

What does git rm -- cached do?

The Git rm –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you'll still have a copy of the file locally. The file will be removed from the index tracking your Git project.

How do I revert git rm cached?

If you've run only git rm -r --cached , try doing a git reset HEAD . from within your repo root. If you did a git commit -m "msg" after doing a git rm -r --cached , i.e., you committed the changes, then do git reset HEAD~1 to undo your last commit.

What is the difference between git rm and rm?

Git rm vs rmThe git rm command removes the file from both the git repository and the local file system. The rm command, on the other hand, only removes the file from the file system.

What is a cached file in git?

The Git cache, also called the staging area or index, contains the working tree directory, including the repository, commits, and branches that would be committed the instance you call the “git commit” command at any point in time.


2 Answers

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.

like image 83
Snowcrash Avatar answered Sep 30 '22 08:09

Snowcrash


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.

like image 36
Chris Moschini Avatar answered Sep 30 '22 09:09

Chris Moschini