Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git ignore all files in a directory except one?

Tags:

git

I know there are many questions on how to ignore directories, and I usually haven't had any problem until now, but now I'm stuck with something I don't understand.

Here is my directory structure:

/src
/war
  com.example.MyProject/
  WEB-INF/
    classes/
    deploy/
    lib/

I want to ignore content of directories classes/, deploy/ and com.example.MyProject/, and here is my .gitignore file:

*.log
war/WEB-INF/classes/
war/WEB-INF/deploy/
com.example.MyProject/

Files under com.example.MyProject/ are automatically generated, and git ignores all of them except for a file named com.example.MyProject.nocache.js. In fact, when I do git status I get:

# On branch myBranch
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   .gitignore
#       ......
#       modified:   war/com.example.MyProject/com.example.MyProject.nocache.js

Why does git refuse to ignore that single file in that directory? What am I doing wrong?

like image 621
MarcoS Avatar asked Sep 30 '11 09:09

MarcoS


2 Answers

If a file is already being tracked by Git, adding the file to .gitignore won't stop Git from tracking it. You’ll need to do git rm --cached <file> to keep the file in your tree and then ignore it

like image 112
Fredrik Pihl Avatar answered Nov 14 '22 22:11

Fredrik Pihl


If the file has already been added to the index, it will still stay in there, even though you add it to the .gitignore file.

Try removing the file from the git index:

git rm --cached war/com.example.MyProject/com.example.MyProject.nocache.js

The --cached option makes sure that the file stays in your folder and just gets removed from the index

like image 20
klaustopher Avatar answered Nov 14 '22 22:11

klaustopher