.gitignore not working for file in subdirectory

When we believe .gitignore does NOT work, one reason could be those files are actually commited. So the .gitignore cannot ignore it!

from this link https://www.atlassian.com/git/tutorials/saving-changes/gitignore the "git check-ignore -v pesty.file.name" can help to debug .gitignore. This is a very useful way to debug. There are probably more other cases causing .gitignore NOT working. If everyone could share their failed case, then how to make it work, the knowledge base can keep growing. this "check-ignore -v" is our friend.


Well, I think it is happening because of the extra space in the ignore rule, due to the comment you've added just afterwards and because of them the pattern mismatches. Verified on git version 1.8.3.2, Ubuntu 13.10

The following entry in .gitignore works all right (without any extra space)

.DS_Store

While neither of these work (Have space after the rule)

*.DS_Store # OS X Finder cache files
.DS_Store # OS X Finder cache files

My guess is, your PROJECT_DIR/.DS_Store is already checked into the repo, hence doesn't show up in git status, are you sure your PROJECT_DIR/.DS_Store is not already a part of the repo?

Do a git log .DS_Store and see if any commits are thrown up.

If yes, use the first ignore rule that works, and remove the exisitng .DS_Store like

git rm --cached .DS_Store
git commit -m "msg"

EDIT

You are entering the comments in the wrong format, they have to be put at the beginning of the file/ignore rules. Check this.

I verified your other ignore rule with comment - *.swp, and it doesn't work either.

From git-scm page on gitignore

Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome)