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. Here's what I've done:
Add to .gitignore in root of project:
node_modules
Run the following:
git rm -r --cached node_modules
git commit -a -m "removed node_modules"
git push origin master
So far so good, this removes the directory from the remote repository. The problem is when I later run git status
it tells me the node_modules directory is untracked and keeps adding it back on future commits.
What am I missing and/or how do I find the root of my problem?
From here:
The git add command will not add ignored files by default. ... The git add command can be used to add ignored files with the -f (force) option.
Additional information from comments:
I am tracking .gitignore file.
git check-ignore node_modules/
returns node_modules/ as expected.
No use of submodules.
Update:
I've created a sample that appears to replicate the issue following the steps above:
https://github.com/awhitehouse104/SampleRepo
Resolution:
To summarize the answer and comments from below, the issue was in the encoding of my .gitignore file. I had used echo 'node_modules' > .gitignore
to create the file on windows 8 and it came out as UTF-16 with BOM (according to answer below). After a few google searches, it seems this is the default encoding with powershell and I can confirm that saving as UTF-8 seems to have resolved the issue.
tldr; Probably don't use this method of creating .gitignore files or be prepared to change the encoding
echo 'node_modules' > .gitignore
Add the file to git ignore, then
Another approach if you don't want to use git rm --cached
Also note the distinction between node_modules and node_modules/ which you seem to have correct. (Thanks umläute for the note on this)
I had a similary issue. Making sure my encoding was ANSI and the line endings were Unix(LF) fixed my issue.
The origin has to be bare. Here is a little bash to demonstrate it. Feel free to edit it, if it does not represent your use case.