100

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?

Community
  • 1
  • 1
WinWin
  • 7,493
  • 10
  • 44
  • 53

2 Answers2

154

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.

random
  • 9,774
  • 10
  • 66
  • 83
Nic
  • 13,287
  • 7
  • 40
  • 42
  • 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 Jul 08 '11 at 00: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 Jul 08 '11 at 01: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 Jul 08 '11 at 02:38
  • Eclipse Egit ALERT: I tried this with the bash and committed with the GUI. The problem recurred. Learning: commit with bash/shell when doing above. :) Cheers! – Dheeraj Bhaskar Apr 20 '13 at 08:10
  • 10
    I've tried `git rm -r --cached ` 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 Feb 13 '15 at 21:41
  • 3
    @haopei: It happen when your bin directory is empty, or it's untracked folder. Pls check by git status. – nofomopls Aug 12 '15 at 04:24
  • Thanks @ducanhng -- Add untracked folder before ignoring it by `git add folder/.` – mujaffars Feb 19 '16 at 13:50
  • worked for me it already assumes your at the root directory so you can literally put a folder name or file within the parent folder and it will remove it. – Omar Feb 18 '19 at 07:33
11

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

yaya
  • 7,675
  • 1
  • 39
  • 38