git add adding ignored files

2019-03-10 13:28发布

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

标签: git gitignore
10条回答
贪生不怕死
2楼-- · 2019-03-10 14:21

Add the file to git ignore, then

git update-index --assume-unchanged <file>
查看更多
Rolldiameter
3楼-- · 2019-03-10 14:29

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)

查看更多
放我归山
4楼-- · 2019-03-10 14:29

I had a similary issue. Making sure my encoding was ANSI and the line endings were Unix(LF) fixed my issue.

查看更多
The star\"
5楼-- · 2019-03-10 14:31

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.

#!/bin/bash

rm -rf /tmp/git_test
mkdir /tmp/git_test/
git init --bare /tmp/git_test/primary_repo

# populate repo
cd /tmp/git_test/
git clone ./primary_repo ./secondary_repo
cd secondary_repo
echo hi_bob > some_content
mkdir node_modules
echo hi_dave > node_modules/moduleA
git add .
git commit -m "populated primary"
git push

# do the removal in tertiary 
cd /tmp/git_test/
git clone ./primary_repo ./tertiary_repo
cd tertiary_repo
echo node_modules >> .gitignore
git add .gitignore
git rm -r --cached node_modules
git commit -a -m "removed node_modules"
git push origin master
rm -r node_modules
echo --------------------------------
echo git status:
git status
查看更多
登录 后发表回答