MarcoS
MarcoS

Reputation: 13564

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

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?

Upvotes: 6

Views: 2533

Answers (2)

klaustopher
klaustopher

Reputation: 6941

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

Upvotes: 5

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

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

Upvotes: 6

Related Questions