I\'m trying to remove a previously tracked directory from git, which works, but it\'s being added back with each subsequent git add .
, git add -A
, etc.
I've found a way to add files to .gitignore even if they have already been commited once. It's a bit brutal but it works great.
Add the different files to your .gitignore
Move these files out from your git project
Rescan & StageChanged, these operation will tell you that these files have been deleted
Commit
Move back your files to your project, they won't be tracked anymore !
I created a repository to try and duplicate your issue, and I got the first answer from http://www.stackoverflow.com/questions/11451535/gitignore-not-working to work.
Here's my repo if you are curious: https://github.com/IAMZERG/so_project_gitignore
Try adding this to the .gitignore instead:
**/directory_to_remove
After that, run git rm --cached directory_to_remove -r
git status should show that you deleted a bunch of files in the directory you are trying to remove. Then, commit and push to your remote, and everything should be golden... Maybe?
Add the file to git ignore, then
git update-index --assume-unchanged <file>
I had a similary issue. Making sure my encoding was ANSI and the line endings were Unix(LF) fixed my issue.
You probably have a negative rule (include-again rule, the one that starts with an !
) in your .gitignore
file somewhere after the node_modules
line.
git check-ignore
has a bug/ambiguity in the docs. You expect that if git check-ignore node_modules/
prints node_modules/
, then node_modules/
is ignored. But actually it prints a pathname if that pathname matches any ignore pattern - positive or negative. The only way to be sure is to use the -v
(--verbose
) option, which will make git check-ignore
print the matching pattern.
Moreover, if git check-ignore -v
says a directory is ignored, it doesn't necessarily mean that all files in that directory are ignored. Example repo:
/
.git/
.gitignore
node_modules/
bar
foo
$ cat .gitignore
/node_modules/*
!/node_modules/foo
$ git check-ignore -v node_modules/
.gitignore:1:/node_modules/* node_modules/
^ positive pattern => ignored
$ git check-ignore -v node_modules/foo
.gitignore:2:!/node_modules/foo node_modules/foo
^ negative pattern => not ignored
$ git add -A
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: node_modules/foo
#
So if git check-ignore -v node_modules/
says node_modules/
is ignored, do git add -A node_modules/
and then run git check-ignore -v --no-index
against individual files that got added, to discover why they were added.
Update: I didn't expect that: your .gitignore
file is in "UTF-16 with BOM (byte order mark)" encoding:
$ cat .gitignore | hexdump -vC
00000000 ff fe 6e 00 6f 00 64 00 65 00 5f 00 6d 00 6f 00 |..n.o.d.e._.m.o.|
00000010 64 00 75 00 6c 00 65 00 73 00 0d 00 0a 00 |d.u.l.e.s.....|
That's why git probably can't handle it. Save the file in UTF-8 without BOM, that should fix the problem. But I also suggest filing a bug report against git check-ignore
- in this corner case its output is clearly not consistent with what git actually ignores.
Another approach if you don't want to use git rm --cached
rm -Rf node_modules
git add -u
git commit -m "stop tracking node_modules"
npm install
# done
Also note the distinction between node_modules and node_modules/ which you seem to have correct. (Thanks umläute for the note on this)