.gitignore Not Respected by Claude — Fix (2026)
The Error
warning: Claude Code staged 'dist/bundle.js' which is listed in .gitignore
warning: Claude Code staged '.env' which is listed in .gitignore
Changes to be committed:
new file: dist/bundle.js
new file: .env
This appears when Claude Code stages or commits files that should be excluded by .gitignore, potentially exposing secrets or adding build artifacts to the repository.
The Fix
git rm --cached dist/bundle.js .env
git commit -m "Remove files that should be gitignored"
- Remove the accidentally tracked files from Git’s index (keeps them on disk).
- Verify
.gitignorehas the correct patterns. - Check that the files were not previously tracked before the
.gitignorerule was added.
Why This Happens
Git’s .gitignore only prevents untracked files from being staged. If a file was tracked before being added to .gitignore, Git continues to track it. Claude Code’s git add commands follow Git’s standard behavior, so it stages previously tracked files regardless of .gitignore rules. Additionally, Claude Code’s Write tool creates files on disk without consulting .gitignore, and a subsequent git add . picks them up.
If That Doesn’t Work
Check if the file was previously tracked:
git log --oneline --follow -- dist/bundle.js
Verify .gitignore patterns are correct:
git check-ignore -v dist/bundle.js
Remove all ignored files from the index:
git rm -r --cached .
git add .
git commit -m "Reset index to respect .gitignore"
Prevention
# CLAUDE.md rule
Never run 'git add .' or 'git add -A'. Always add specific files by name. Before staging, verify files are not in .gitignore. Never stage .env, dist/, node_modules/, or build artifacts.
Related Error Messages
This fix also applies if you see these related error messages:
fatal: not a git repositoryerror: failed to push some refsfatal: refusing to merge unrelated historiesError: Claude Code requires Node.js 18 or laterSyntaxError: Unexpected token '??' — Node 14 detected
Frequently Asked Questions
Why does Claude Code require git?
Claude Code uses git for several core operations: tracking file changes, creating commits, reading blame information, searching history with git log, and managing branches. Without git, these operations fail and Claude Code falls back to less efficient alternatives.
Can Claude Code work in a non-git directory?
Yes, but with reduced functionality. File search and editing work normally, but version control operations (commit, diff, blame) are unavailable. Claude Code displays a warning when opened in a directory without git initialization.
How do I prevent Claude Code from making unwanted git operations?
Add rules to your CLAUDE.md: Do not create commits automatically. Do not run git push. Always ask before any git operation that modifies history. Claude Code respects these constraints and asks for confirmation before proceeding.
What Node.js version does Claude Code require?
Claude Code requires Node.js 18 or later. Node.js 20 LTS is recommended for the best compatibility and performance. Check your version with node --version.
Related Guides
Try it: Paste your error into our Error Diagnostic for an instant fix.
- Terminal Emulator Rendering Artifacts — Fix (2026)
- How to Use Thirdweb SDK Workflow (2026)
- Python Virtualenv Not Activated — Fix (2026)
- Claude Code Offline Mode Setup (2026)
Git Operations in Claude Code: Safety Checklist
Claude Code can execute git commands, which makes safety guardrails important:
Before any destructive operation: Always check git status and git stash list to confirm there are no uncommitted changes that could be lost.
Branch management: Claude Code should create feature branches for non-trivial changes rather than committing directly to main. Use the pattern git checkout -b claude/feature-name to clearly identify AI-generated branches.
Commit message conventions: Configure your preferred commit format in CLAUDE.md. Claude Code follows the format you specify. Common formats: Conventional Commits (feat: add user search), Angular style, or simple descriptive messages.
Common Git Mistakes Claude Code Makes
-
Amending the wrong commit. If a pre-commit hook fails, Claude Code sometimes uses
--amendon the next attempt, which modifies the previous (successful) commit instead of creating a new one. Configure CLAUDE.md with: “Never use git commit –amend. Always create new commits.” -
Force pushing to shared branches. Claude Code may suggest
git push --forceto resolve push rejections. AddBash(git push --force*)to your deny list in settings.json. -
Committing generated files. Without guidance, Claude Code may commit
dist/,node_modules/, or.envfiles. Ensure your.gitignoreis complete and add a pre-commit hook that checks for these.