PricingSign in52Download

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
  1. See what got in
  2. Ignore them first
  3. Stop tracking them, keep the files
  4. Not pushed yet: fix the commit itself
  5. Already pushed: what teammates see
  6. Why a file is ignored
  7. Other things agents commit
  8. Stop it happening again
  9. Where Joxo fits
  10. Frequently asked questions
  11. How do I remove node_modules from git without deleting it?
  12. Why is a file still tracked after I added it to .gitignore?
  13. Will git rm --cached delete files for my teammates?
  14. Does git rm --cached make the repository smaller?
  15. How do I see which rule ignores a file?
  16. How do I find which folders have the most tracked files?
  17. Should .env.example be in git?
  18. How do I stop the agent committing node_modules again?
  19. 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_modules, 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#

bash
git show --stat HEAD
Add 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:

bash
git ls-files | cut -d/ -f1 | sort | uniq -c | sort -rn | head -5
     12 node_modules
      3 src
      1 package.json
      1 dist
      1 .env

A 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#

bash
printf 'node_modules/\ndist/\n.env\n' >> .gitignore

Adding them to .gitignore alone does nothing for files git already tracks. git status shows only the new .gitignore.

Stop tracking them, keep the files#

bash
git rm -r --cached node_modules dist .env
rm '.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:

bash
git add .gitignore
git commit -m "Stop tracking build output and .env"

Check nothing is left:

bash
git ls-files | grep -c node_modules
0

Not 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:

bash
git commit --amend --no-edit

If 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.json

Tell 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:

bash
git check-ignore -v node_modules/left-pad/index.js
.gitignore:1:node_modules/	node_modules/left-pad/index.js

No output means no rule matches it.

Other things agents commit#

node_modules 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.example

The 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 .gitignore before 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.

Something missing or wrong? Support reaches a person.