9

This is the content of my .gitignore file, which is at the root of my repository:

# grails stuff to ignore
target
*.iml
*.ipr
.idea
*.log
*.swp   # vi swap files
.DS_Store  # OS X Finder cache files

# cocoapods stuff to ignore
Pods
Podfile.lock

The .DS_Store file in my project root directory is correctly ignored, but git status gives:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    ios/.DS_Store

nothing added to commit but untracked files present (use "git add" to track)

So why is ios/.DS_Store not ignored?

Working on OS X 10.9.2, using the git version that Apple supplies: "git version 1.8.5.2 (Apple Git-48)"

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Bart van Kuik
  • 4,704
  • 1
  • 33
  • 57

2 Answers2

14

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)

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 1
    YES!!!! You are right, you can't add comments after the filename/pattern. Boy that had me stumped for a while. – Bart van Kuik Apr 12 '14 at 06:52
  • @SilverNightaFall this worked for me too. Given directory `foo/`, I did `git rm --cached -r foo/` (the `-r` is required for is to be recursive, because it is a directory). Then I used `git add foo/`, and everything was good. – Jacob Stamm Sep 08 '18 at 21:17
3

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.

  • This is such a good point (and somewhat obvious when you think about it!) - thank you for raising :-) – hobailey Mar 18 '22 at 16:36