Why is .gitignore not working?

Most surprises come from a file that Git already tracks, a rule in another location, or a pattern that does not match the path you think it does.

1. Check whether Git already tracks the file

Ignore rules apply to intentionally untracked files. If the file was committed earlier, adding a pattern will not remove it from Git’s index.

git ls-files -- path/to/file

If this prints the path, the file is tracked. Follow the tracked-file guide:

How do I ignore files Git already tracks?

2. Ask Git which rule matched

Run the checker from your repository, using the actual path relative to that repository. The output points to the source ignore file and line.

git check-ignore -v -- path/to/file

Use --no-index when testing a file that is already tracked:

git check-ignore -v --no-index -- path/to/file

3. Review pattern location and order

A leading slash anchors a pattern to the directory containing that .gitignore file. A trailing slash restricts it to directories. Later matching patterns at the same precedence level can override earlier ones. A pattern beginning with ! can re-include a path, but not a file inside a directory Git has already excluded from traversal.

Why does ! fail to keep a file inside an ignored directory?

4. Check other ignore sources

Git also reads .git/info/exclude and the global file configured with core.excludesFile. A file may therefore be hidden even when your project’s .gitignore does not mention it. Check the global configuration with:

git config --get core.excludesFile

Why is a file ignored by global Git rules?

For exact behavior and further details, consult the official references: Git documentation