Git add added ignored files

I am trying to remove a previously tracked directory from git that works, but it is added back with each subsequent git add . , git add -A etc. Here is what I did:

Add to .gitignore to the project root:

 node_modules 

Do the following:

 git rm -r --cached node_modules git commit -a -m "removed node_modules" git push origin master 

So far, so good that it removes a directory from a remote repository. The problem is that when I later run git status , it tells me that the node_modules directory is not checked and continues to add it to future commits.

What am I missing and / or how can I find the root of my problem?

From here :

The git add command does 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 the comments:

I am tracking a .gitignore file.

git check-ignore node_modules/ returns node_modules / as expected.

No use of submodules.

Update:

I created a pattern that seems to replicate the problem by following the steps above:

https://github.com/awhitehouse104/SampleRepo

Resolution:

To summarize the answer and comments below, the problem was the encoding of my .gitignore file. I used echo 'node_modules' > .gitignore to create the file on windows 8 and it appeared as UTF-16 with the specification (as per the answer below). After a few google searches, it seems like this is the default encoding with powershell, and I can confirm that saving, since UTF-8 seems to have solved the problem.

TL; DR; Probably do not use this method of creating .gitignore files or be prepared to change the encoding

echo 'node_modules' > .gitignore

+43
git gitignore
Sep 04 '15 at 15:14
source share
9 answers

You probably have a negative rule (include-again rule that starts with ! ) In your .gitignore file somewhere after the node_modules line.

git check-ignore has an error / ambiguity in the docs. You expect that if git check-ignore node_modules/ prints node_modules/ , then node_modules/ ignored. But in fact, it prints a path if that path matches any ignore pattern - positive or negative. The only way to make sure is to use the -v option ( --verbose ), which will make git check-ignore print the appropriate template.
Moreover, if git check-ignore -v says that the directory is ignored, this does not necessarily mean that all files in this directory are ignored. Repo example:

 / .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 that node_modules/ ignored, do git add -A node_modules/ and then run git check-ignore -v --no-index on the individual added files to find out why they have been added.




Update: I did not expect that: your .gitignore file is in "UTF-16 with BOM (byte order)":

 $ cat .gitignore | hexdump -vC 00000000 ff fe 6e 00 6f 00 64 00 65 00 5f 00 6d 00 6f 00 |..node_.mo| 00000010 64 00 75 00 6c 00 65 00 73 00 0d 00 0a 00 |dules....| 

This is why git probably won't handle it. Save the file in UTF-8 without a specification that should fix the problem. But I also suggest reporting an error regarding git check-ignore - in this corner case, its output is clearly not consistent with what git actually ignores.

+12
Sep 13 '15 at 0:54
source share

You can do

 git check-ignore -v --no-index path/with/unexpected/result 

to find out why git add added or not added this path.

git check-ignore docs .

In particular, you want to check what is actually being added, not the directory.

next, do find . -name .git find . -name .git . Submodules are nested repositories, .gitmodules and the submodule command are convenient, but they just help them.

+6
Sep 12 '15 at 16:10
source share

Add file to git ignore, then

 git update-index --assume-unchanged <file> 
+5
Sep 12 '15 at 13:52
source share

Here is what I do to ignore the node_modules folder after tracking it.

  • Create a .gitignore file and add node_modules to it.

  • Commit the .gitignore file. After this point, everything you update in the node_modules folder will not appear in git status .

  • But this does not delete what we already have on the repo in the node_modules folder. So, now we need to delete everything that we did earlier.

    To do this, use git rm --cached node_modules -r

  • Now git status will show that the files are deleted.

  • Use the git commit -m "node_modules removed" with any message.

Now everything should be removed from the repo, and future changes will not be tracked.

+1
09 Sep '15 at 18:14
source share

Another approach if you do not 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 difference between node_modules and node_modules /, which seems to have the correct meaning. (Thanks, thanks for the note about this)

0
Sep 09 '15 at 18:38
source share

gitignore - Specifies intentionally unverified files to ignore.

$ HOME / .config / git / ignore, $ GIT_DIR / info / exclude, .gitignore

Each line in the gitignore file indicates a pattern. When deciding whether to ignore the path, Git usually checks gitignore templates from several sources with the following order of priority: from highest to lowest (within the same priority level, the last matching pattern decides the outcome):

To ignore the entire directory, you must use /** ,

The final / ** matches everything inside. For example, abc / ** matches all files inside the "abc" directory relative to the location of the .gitignore file with infinite depth.

or

You can ignore the entire directory by adding this line to your root .gitignore file:

/ DIR_NAME

Instead, you can add the / logs / .gitignore file containing this:

[^.] *

The directory will remain in your repo, but all files inside / will be ignored. Easily!

Below are the steps you need to follow.

  • Delete it from the project directory (without deleting it):

    git rm --cached folder / *

  • If you don't have .gitignore yet, you can do it right inside the project folder:
  • Project / .gitignore. Place the / * folder (or any template that you think is better from the list above) in the .gitignore Commit:
  • git commit -m "message".
  • Click on your change on github.

An example is to exclude everything except the specific directory foo / bar (note / * - without a slash, the wildcard will also exclude everything inside foo / bar):

 $ cat .gitignore # exclude everything except directory foo/bar /* !/foo /foo/* !/foo/bar 
0
12 sept '15 at 13:43 on
source share

I created a repository to try to duplicate your problem, and I got the first answer from http://www.stackoverflow.com/questions/11451535/gitignore-not-working .

Here's my repo if you're interested: https://github.com/IAMZERG/so_project_gitignore

Try adding this instead of .gitignore:

** / directory_to_remove

After that run git rm --cached directory_to_remove -r

The git status should indicate that you have deleted a bunch of files in the directory you are trying to delete. Then, complete and click on your remote, and everything should be gold ... Maybe?

0
Sep 16 '15 at 6:08
source share

I had a similar problem. Making sure my encoding was ANSI and the line ending was Unix (LF) fixed my problem.

0
Jan 26 '17 at 21:34 on
source share

The origin must be bare. Here is a little bash to demonstrate this. 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 
-one
Sep 10 '15 at 10:00
source share



All Articles