103

I've learned how to exclude an entire directory in git (add a line bin/ to .gitignore). And I've learned how to ignore files "after the fact" (i.e. after they have been added to git):

git rm --cached <filename>

How do I ignore an entire directory (e.g. bin/) after it has been added to a Git repo?

I tried git rm --cached bin/ but all I received was the error:

fatal: pathspec 'bin/' did not match any files

When I tried (at the root directory, where .git exists) git rm --cached MyProj/bin/ the error is different:

fatal: not removing 'MyProj/bin/' recursively without -r

What does this mean and will I need to commit and/or branch this now?

2
  • 5
    Note the -r tag in my answer. You have to use that for recursive :)
    – Nic
    Commented Jul 8, 2011 at 0:40
  • @melee It worked! But now I have another question(s). See update. :)
    – WinWin
    Commented Jul 8, 2011 at 0:49

2 Answers 2

156

I was able to get this working with git rm -r --cached bin/ (note the recursive -r)in the root of the repo - are you talking about finding the bin directories and untracking them?

You will have to commit before the exclusion is reflected.

I just saw that you were on Windows. This was in Terminal on OSX, just a heads up.

8
  • Yes, I am on windows but I am using Cygwin which provides an entire Unix-like experience... See my update above. I am not sure I understand what you mean by "finding". I really have only one bin/ subdirectory under my project. The .git directory however is a sibling of my project's directory (i.e. entire git tracking starts one directory above, which could contain several projects). Thanks and +1 for the insights.
    – WinWin
    Commented Jul 8, 2011 at 0:42
  • Hmm. Here's an idea; touch or edit one of the files in the bin dir and then try got status. If that is working, they should no longer be tracking.
    – Nic
    Commented Jul 8, 2011 at 1:21
  • @WinWin sorry about that, I was on my phone and missed part of your edit. Glad you were able to get it figured out!
    – Nic
    Commented Jul 8, 2011 at 2:38
  • 11
    I've tried git rm -r --cached <folder_name> in the root folder (which contains the .git folder), and I still receive the error fatal: pathspec 'folder_name' did not match any files. This is especially strange since I had been able to press 'tab' to autocomplete the folder's name. I am using Terminal. Any ideas?
    – hyang123
    Commented Feb 13, 2015 at 21:41
  • 3
    @haopei: It happen when your bin directory is empty, or it's untracked folder. Pls check by <code>git status</code>.
    – nofomopls
    Commented Aug 12, 2015 at 4:24
12

On windows:

git rm -r --cached ./FOLDERNAME/

And then do the other stuffs. (add and commit and push)

(Note that on windows you should use ./ before the folder name, just like above.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.