I lurk on the #git IRC channel on FreeNode. There’s a common problem I see fairly frequently. It usually starts something like this:

I’m trying to add three auto-generated files to my gitignore file, but they’re still showing up as Modified.

What’s happened here is a fundamental misunderstanding of what gitignore does.

In git, there are two types of files: files that git tracks and files git does not track. Once you’ve told git to track a file (with git add $FILE), it is tracked until you tell git to stop tracking and delete it (with git rm $FILE).

Here’s the gotcha with gitignore: it only applies to untracked files. Once a file is tracked, gitignore is totally irrelevant. What the gitignore is meant for is stuff like git status and git add $DIRECTORY. If a file is gitignored, it will not show up in the “untracked” section of the git status output. If you add a folder with something like git add $DIRECTORY, then the gitignored files will not be added (but all the other untracked, but not ignored files will be added).


As an aside, one of the common reasons for running into this mistake is trying to put config files into git. Someone wants to commit some kind of config file, but each developer is expected to edit that file for their development station. Or maybe they want one version of the file for testing and a different version for production. (Which probably contains API keys, which is it’s own section of Bad Idea.) In that case, there’s a couple different ways it could be handled, but I might save those for a future post (or link to one of the existing ones elsewhere).