Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore not working for file in subdirectory

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)"

like image 290
Bart van Kuik Avatar asked Apr 11 '14 15:04

Bart van Kuik


1 Answers

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)

like image 155
Anshul Goyal Avatar answered Oct 01 '22 20:10

Anshul Goyal