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

I know that there are a lot of questions about how to ignore directories, and until now I have usually had no problems, 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 the contents of the 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/ 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 this single file in this directory? What am I doing wrong?

+6
source share
2 answers

If the file is already tracked by Git, adding the file to .gitignore will not stop Git from tracking it. You need to do git rm --cached <file> to save the file in your tree and then ignore it

+5
source

If the file has already been added to the index, it will still remain there, even if 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 ensures that the file remains in your folder and is simply deleted from the index

+5
source

Source: https://habr.com/ru/post/898362/


All Articles