git
gitignore
directories
troubleshooting
version control

.gitignore is not ignoring directories

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When .gitignore seems to ignore nothing, the usual reason is that files were already tracked before the ignore rule was added. Git ignore rules affect untracked files only. To fix directory ignore problems, verify pattern syntax and remove tracked entries from the index without deleting local files.

Verify Ignore Pattern Syntax

A few pattern rules matter for directories:

  • build/ ignores a directory named build at any level when not tracked
  • /build/ targets only top-level build
  • **/build/ matches recursively in nested paths

Example .gitignore:

gitignore
1# Ignore build outputs
2build/
3
4# Ignore environment folders
5.venv/
6node_modules/
7
8# Ignore logs
9*.log

After edits, test a file path with git check-ignore.

bash
git check-ignore -v path/to/build/output.txt

This command shows which rule matched.

Remove Already Tracked Files from Index

If files were committed before ignore rules existed, Git keeps tracking them until you untrack explicitly.

bash
1# Remove tracked directory from index only
2git rm -r --cached build/
3
4# Re-add everything so ignore rules apply
5git add .
6git commit -m "Stop tracking build directory and apply ignore rules"

--cached keeps local files on disk and updates only Git index state.

Debug Conflicting Ignore Sources

Rules can come from multiple places:

  • repository .gitignore
  • .git/info/exclude
  • global ignore file configured by user

Inspect global ignore path:

bash
git config --get core.excludesfile

Unexpected behavior often comes from a conflicting global rule.

Handle Nested Repositories and Submodules

If a directory is a nested Git repository or submodule, parent ignore rules may not behave as expected. Check for inner .git metadata and submodule configuration before troubleshooting patterns.

bash
find . -name .git -type d
cat .gitmodules 2>/dev/null || true

When submodules are involved, manage ignore rules in the correct repository scope.

Practical Cleanup Workflow for Existing Repositories

In mature repositories, ignored artifacts often exist in many directories. Use a deliberate cleanup sequence to avoid accidental removals.

bash
1# Preview tracked files that match ignored patterns
2git ls-files -ci --exclude-standard
3
4# Remove ignored tracked files from index only
5git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
6
7# Re-stage and review
8git add .
9git status

This approach scales better than hand-removing each directory and ensures ignore rules are applied consistently.

After cleanup, add a CI check that fails when generated directories are committed. Preventing regression is easier than repeating cleanup across branches.

A simple guard can scan for known build folders and fail early with a clear message. Teams that add this check avoid repeated churn from accidentally committed artifacts. It also keeps pull requests focused on source changes instead of noisy generated files.

For monorepos, keep shared ignore patterns centrally documented so each package does not reinvent conflicting rules.

Review ignore rules quarterly as build tooling changes.

Keep the rationale near each non-obvious pattern for maintainability.

Common Pitfalls

A common pitfall is adding an ignore rule and expecting tracked files to disappear from git status automatically. Index cleanup is required.

Another issue is using Windows path separators in .gitignore. Use forward slashes in rules.

Developers also forget to commit .gitignore updates, so teammates keep tracking generated directories.

Finally, broad rules such as * can hide too much and make new source files invisible. Keep ignore patterns specific.

Summary

  • .gitignore affects only untracked files.
  • Use correct directory patterns and validate with git check-ignore -v.
  • Untrack existing files with git rm --cached so rules can take effect.
  • Check repository, local exclude, and global ignore sources for conflicts.
  • Keep patterns explicit to avoid accidental hiding of important files.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.