.gitignore is ignored by Git

My .gitignore file seems to be being ignored by Git - could the .gitignore file be corrupt? Which file format, locale or culture does Git expect?

My .gitignore:

# This is a comment
debug.log
nbproject/

Output from git status:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       debug.log
#       nbproject/
nothing added to commit but untracked files present (use "git add" to track)

I would like debug.log and nbproject/ not to appear in the untracked files list.

Where should I start looking to fix this?


Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore.

WARNING: First commit or stash your current changes, or you will lose them.

Then run the following commands from the top folder of your Git repository:

git rm -r --cached .
git add .
git commit -m "fixed untracked files"

If it seems like Git isn't noticing the changes you made to your .gitignore file, you might want to check the following points:

  • There might be a global .gitignore file that might interfere with your local one
  • When you add something into a .gitignore file, try this:

    git add [uncommitted changes you want to keep] && git commit
    git rm -r --cached .
    git add .
    git commit -m "fixed untracked files"
    
  • If you remove something from a .gitignore file, and the above steps maybe don't work,if you found the above steps are not working, try this:

    git add -f [files you want to track again]
    git commit -m "Refresh removing files from .gitignore file."
    
    // For example, if you want the .java type file to be tracked again,
    // The command should be:
    //     git add -f *.java
    

Fixed. OK, I created the .gitignore file in Notepad on Windows and it wasn't working. When I viewed the .gitignore file on Linux it looked like organised gibberish - perhaps Notepad had written out Unicode rather than ASCII or whatever 8-bit is.

So I rewrote the file on my Linux box, and when I pulled it back into Windows it works fine! Hurrah!


Without adding another commit to your project, one line will be enough to make .gitignore work as it is supposed to:

git rm -r --cached debug.log nbproject

This will remove them from the repository, but still keep them physically. In plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. You may find a better explanation here.