Blog / git
A coding agent committed node_modules: stop tracking it
An agent ran git add -A and committed node_modules, build output or .env. Untrack them without deleting them, ignore them, and know what teammates will see.
On this page
- See what got in
- Ignore them first
- Stop tracking them, keep the files
- Not pushed yet: fix the commit itself
- Already pushed: what teammates see
- Why a file is ignored
- Other things agents commit
- Stop it happening again
- Where Joxo fits
- Frequently asked questions
- How do I remove node_modules from git without deleting it?
- Why is a file still tracked after I added it to .gitignore?
- Will git rm --cached delete files for my teammates?
- Does git rm --cached make the repository smaller?
- How do I see which rule ignores a file?
- How do I find which folders have the most tracked files?
- Should .env.example be in git?
- How do I stop the agent committing node_modules again?
- Try it with your team
The agent ran git add -A in a project with no .gitignore for those folders, and its commit has thousands of files from node_, a dist folder, maybe a .env. You want them out of git but still on your disk. We ran every command with git 2.43 on a scratch repository.
See what got in#
git show --stat HEADAdd search page
.env | 1 +
dist/app.js | 1 +
node_modules/left-pad/index.js | 1 +
node_modules/left-pad/package.json | 1 +
4 files changed, 4 insertions(+)With thousands of files, --stat scrolls for pages. Count tracked files per top-level folder instead:
git ls-files | cut -d/ -f1 | sort | uniq -c | sort -rn | head -5 12 node_modules
3 src
1 package.json
1 dist
1 .envA dependency or build folder near the top is the one to look at first.
If .env or any key is in there, treat the keys as leaked once pushed and rotate them; see what to do when an agent commits a secret.
Ignore them first#
printf 'node_modules/\ndist/\n.env\n' >> .gitignoreAdding them to .gitignore alone does nothing for files git already tracks. git status shows only the new .gitignore.
Stop tracking them, keep the files#
git rm -r --cached node_modules dist .envrm '.env'
rm 'dist/app.js'
rm 'node_modules/left-pad/index.js'
rm 'node_modules/left-pad/package.json'--cached removes them from git only. The folders stay on disk and your app still runs. Commit the removal with the ignore file:
git add .gitignore
git commit -m "Stop tracking build output and .env"Check nothing is left:
git ls-files | grep -c node_modules0Not pushed yet: fix the commit itself#
If the agent's commit is still only on your computer, run the same git rm -r --cached and git add .gitignore, then fold them into that commit so the files never enter history:
git commit --amend --no-editIf the commit is further back, see fix an older commit.
Already pushed: what teammates see#
The files stay in history, so the repository stays as big as it was. When a teammate pulls your removal commit, git deletes those files from their disk, because they were tracked and now aren't:
before pull: .env dist node_modules package.json
after pull: .gitignore package.jsonTell them to run npm install (or their build) again, and to keep a copy of their own .env before pulling. To shrink the history as well, see a coding agent committed a large file.
Why a file is ignored#
When something should be ignored and isn't, or the other way round:
git check-ignore -v node_modules/left-pad/index.js.gitignore:1:node_modules/ node_modules/left-pad/index.jsNo output means no rule matches it.
Other things agents commit#
node_ is the big one, but the same git rm -r --cached fixes all of these. Lines worth having in most .gitignore files:
node_modules/
dist/
build/
coverage/
.venv/
__pycache__/
*.log
.DS_Store
.env
.env.*
!.env.exampleThe last line keeps an .env.example tracked, so teammates know which keys to set. Agents also create scratch files while they work, such as notes, test output or copies of a file with .bak on the end; clean up files a coding agent left covers those.
Check a rule before you commit it: git status --ignored lists what is now ignored, so you can see a pattern didn't catch something you meant to keep.
Stop it happening again#
- Commit a
.gitignorebefore the agent's first run. Start from your language's template and add your build and cache folders. - Tell the agent in your project instructions: "Stage files by name with git add <path>. Never use git add -A or git add ., and never commit .env, node_modules or build output."
- A pre-commit hook can refuse commits that include those paths.
Where Joxo fits#
When several people's agents push to one repository, one stray git add -A lands in everyone's checkout. In Joxo, project rules every agent reads live in one place, so a rule like "never commit node_modules" reaches every teammate's agent at once. Setup is one line pasted into your agent.
Frequently asked questions#
How do I remove node_modules from git without deleting it?#
Add node_modules/ to .gitignore, then run git rm -r --cached node_modules and commit. --cached removes it from git and leaves the folder on disk.
Why is a file still tracked after I added it to .gitignore?#
.gitignore only applies to files git doesn't track yet. Run git rm --cached <file> once to stop tracking it.
Will git rm --cached delete files for my teammates?#
Yes. When they pull the commit, git deletes those files from their working copy. They need to reinstall dependencies or rebuild, and should copy their .env first.
Does git rm --cached make the repository smaller?#
No. The files stay in earlier commits. Rewriting history is the only way to remove them completely.
How do I see which rule ignores a file?#
Run git check-ignore -v <path>. It prints the .gitignore file, line and pattern that match.
How do I find which folders have the most tracked files?#
Run git ls-files | cut -d/ -f1 | sort | uniq -c | sort -rn | head. It counts tracked files per top-level folder, so node_modules or a build folder stands out at once.
Should .env.example be in git?#
Yes. Ignore .env and .env.* but add !.env.example after them, so the example with the key names and no values stays tracked for teammates and agents.
How do I stop the agent committing node_modules again?#
Commit a .gitignore that lists it, and tell the agent in your project instructions to stage files by name rather than with git add -A. A pre-commit hook can refuse the commit if it still happens.