.gitignore Not Working? How to Fix Common Problems

If Git keeps showing a file you added to .gitignore, the file may already be tracked, the rule may not match its path, or another ignore rule may apply. Use these checks to find the cause without deleting your local files.

Practice Git workflows →

1. Check whether Git already tracks the file

Ignore rules apply to untracked files. Adding a file that is already committed to .gitignore does not remove it from Git’s index. Check its status with:

git ls-files -- path/to/file

If the command prints the path, Git is tracking it. To stop tracking it while keeping your local copy, remove it from the index:

git rm --cached -- path/to/file

Then commit the removal and the relevant .gitignore change. For an already-tracked directory, use git rm -r --cached -- path/to/directory. Review git status before committing; the files remain in your working directory, but Git will stage their removal from the repository.

2. Verify that the ignore pattern matches the file

Use git check-ignore -v to see whether a rule matches and which ignore file and line supplied it:

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

If there is no output, Git did not find a matching ignore rule for that path. Try a more specific pattern and test again. For example:

# Ignore this file at the repository root
/debug.log

# Ignore files with this extension in any directory
*.log

# Ignore a directory and its contents
build/

A leading slash anchors a pattern to the directory containing that particular .gitignore. A slash in the middle of a pattern also makes it relative to that ignore file. Patterns without a slash can match names at any directory level.

3. Check nested and global ignore rules

Git can read ignore rules from the repository’s .gitignore, lower-level .gitignore files, .git/info/exclude, and a user-level excludes file. A rule in a nested file is relative to that directory. The verbose output from git check-ignore -v identifies the matching source.

If a broad rule ignores a directory but you need to include one file inside it, Git may need the parent directory to be re-included before a more specific exception can work. Check every relevant rule and test the exact file path rather than guessing.

4. Make sure the file is in the repository you expect

A .gitignore only affects paths in its repository and below its directory. Confirm your current repository root and file status:

git rev-parse --show-toplevel
git status --short -- path/to/file

In a monorepo or nested project, the right ignore file may be at the repository root or nearer to the file. Put the rule where it applies and use the path relative to that ignore file.

.gitignore troubleshooting FAQ

Does .gitignore delete a file from GitHub?

No. It prevents matching untracked files from being added. To stop tracking a committed file without deleting your local copy, use git rm --cached, then commit that change.

Why does git status still show an ignored file?

It is commonly already tracked or the pattern does not match the file’s path. Check tracking with git ls-files and check matching rules with git check-ignore -v.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →