180

unable to remove file that really exists - fatal: pathspec ... did not match any files

I have a file under git control that simply will not be deleted. The failing command is:

$ git rm .idea/workspace.xml
fatal: pathspec '.idea/workspace.xml' did not match any files

Below I list the directory contents, branches, etc. So far I've tried rm from within the directory, and escaping just in case there are funny characters, and I'm really stumped. I searched the web and SO but couldn't find this specifically.

$ git branch -a
* dot-output
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/modelspace
$ 

$ git status
# On branch dot-output
# Untracked files:
# ...

$ ls .idea/
ant.xml         encodings.xml       modules.xml     workspace.xml
compiler.xml        inspectionProfiles  scopes
copyright       libraries       testrunner.xml
dictionaries        misc.xml        vcs.xml

$ ls -al
total 56
drwxr-xr-x  16 matt  staff    544 Apr 10 11:33 .
drwxr-xr-x@ 33 matt  staff   1122 Apr 10 09:40 ..
-rw-r--r--@  1 matt  staff  12292 Apr 10 11:19 .DS_Store
drwxr-xr-x  18 matt  staff    612 Apr 10 11:39 .git
-rw-r--r--   1 matt  staff     98 Mar  6 13:40 .gitignore
drwxr-xr-x  16 matt  staff    544 Apr 10 11:34 .idea
-rw-r--r--   1 matt  staff   1113 Feb 25 11:07 README
...

$ head -n 2 .idea/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">

$ git rm .idea/workspace.xml
fatal: pathspec '.idea/workspace.xml' did not match any files

Update

Answers from @Nevik and @Boris helped a lot. I realized that I was confused by a number of things going on, some of which were exacerbated by IntelliJ IDEA (a tool I generally love, BTW). First, the 'fatal: pathspec' message from git rm is both unhelpful and misleading.

Second, I had the file in .gitignore, but had removed it before asking my question. However, it was also included in IDEA's Ignored Files feature independently of git, and showed as such in the project viewer (instead of untracked, as git status showed). Finally, I had IDEA running while I was experimenting, and it looks like it was recreating the file immediately after my rm.

My takeaway is that if I'm getting confusing Git behavior, make sure to quit IDEA and work solely in the command line (and gitk) while debugging.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Matthew Cornell
  • 4,114
  • 3
  • 27
  • 40

9 Answers9

142

Your file .idea/workspace.xml is not under git version control. You have either not added it yet (check git status/Untracked files) or ignored it (using .gitignore or .git/info/exclude files)

You can verify it using following git command, that lists all ignored files:

git ls-files --others -i --exclude-standard
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
  • That helped a lot, Boris. Thank you! I've updated my question to summarize the things that were going on (IDEA, etc) – Matthew Cornell Apr 11 '13 at 13:16
  • Just a quick note to others: The accepted answer did not work for me, and I was about to go crazy when I noticed that my .gitignore was not at the top level where I thought it was, but down inside one of my modules (Thanks, PyCharm). As soon as I put it where it belonged, problem solved. The docs also talk about this: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring under "Note". – Malik A. Rumi Nov 13 '21 at 21:31
26
$>git add .
$>git rm file_Name  

It works. You add new file using right click -> create new file, and immediately delete it after that. The file would go to the untracked file list.

John_West
  • 2,239
  • 4
  • 24
  • 44
gnganapath
  • 917
  • 12
  • 16
10

I know this is not the OP's problem, but I ran into the same error with an entirely different basis, so I just wanted to drop it here in case anyone else has the same. This is Windows-specific, and I assume does not affect Linux users.

I had a LibreOffice doc file, call it final report.odt. I later changed its case to Final Report.odt. In Windows, this doesn't even count as a rename. final report.odt, Final Report.odt, FiNaL RePoRt.oDt are all the same. In Linux, these are all distinct.

When I eventually went to git rm "Final Report.odt" and got the "pathspec did not match any files" error. Only when I use the original casing at the time the file was added -- git rm "final report.odt" -- did it work.

Lesson learned: to change the case I should have instead done:

git mv "final report.odt" temp.odt
git mv temp.odt "Final Report.odt"

Again, that wasn't the problem for the OP here; and wouldn't affect a Linux user, as his posts shows he clearly is. I'm just including it for others who may have this problem in Windows git and stumble onto this question.

codingatty
  • 2,026
  • 1
  • 23
  • 32
  • 1
    This can happen on Windows if any part of the path is different in case to what is recorded in git. I was able to workaround by using a bash shell in Windows – Rattle Aug 23 '18 at 09:03
3

If your file idea/workspace.xml is added to .gitignore (or its parent folder) just add it manually to git version control. Also you can add it using TortoiseGit. After the next push you will see, that your problem is solved.

Add to git versioning using TortoiseGit

Lord Nighton
  • 1,670
  • 21
  • 15
  • 1
    When someone needs to `rm` a directory or a file, he/she does not need to add it to git if it's not already listed under git version control! – Mohammad Kermani Apr 05 '17 at 10:15
3

Helped me:

  1. git add .
  2. git stash
Mykola
  • 93
  • 1
  • 3
  • 11
0

In my instance, there was something completely odd that I'm not sure what the cause was. An entire folder was committed previously. I could see it in Git, Windows Explorer, and GitHub, but any changes I made to the folder itself and the files in it were ignored. Using git check-ignore to see what was ignoring it, and attempting to remove it using git rm --cached had no impact. The changes were not able to be staged.

I fixed it by:

  1. Making a copy of the folder and files in another location.
  2. I deleted the original that was getting ignored somehow.
  3. Commit and push this update.
  4. Finally, I added the files and folder back and git was seeing and reacting to it as expected again.
  5. Stage and commit this, and you're good to go! :)
Will Strohl
  • 1,646
  • 2
  • 15
  • 32
0

Personally I stumbled on a similar error message in this scenario:

I created a folder that has been empty, so naturally as long as it is empty, typing git add * will not take this empty folder in consideration. So when I tried to run git rm -r * or simply git rm my_empty_folder/ -r, I got that error message.

The solution is to simply remove it without git: rm -r my_empty_folder/ or create a data file within this folder and then add it (git add my_no_long_empty_folder)

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
0

What worked for me was to run:

  1. git add .idea/workspace.xml
  2. git rm -r --cached .idea/*
-6

Move temporarily .gitignore to .gitignore.bck

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45