PricingSign inDownload

Blog / git

Your coding agent committed node_modules or a huge file: undo it

Find the big files in the last commit, take them out before you push, ignore them for good, and add a pre-commit hook that stops the next one.

On this page
  1. How do I see what went into the last commit?
  2. How do I take them out before pushing?
  3. What if the agent made more commits after that one?
  4. How do I stop it happening again?
  5. Does the agent respect the hook?
  6. When several agents commit to one repository
  7. Frequently asked questions
  8. How do I remove node_modules from my last commit?
  9. Does git rm --cached delete my files?
  10. Why is my .git folder still big after removing the file?
  11. How do I stop a coding agent committing large files?
  12. Try it with your team

You asked the agent to fix the build and commit. It did: git add -A, then a commit called "Fix build". The commit also holds node_modules/, a 30 MB bundle from dist/ and maybe a database dump, because nothing told git to ignore them and -A takes everything. Pushed, that bloats the repository for every clone from now on, and a single file over 100 MB is refused by GitHub outright.

The good news: if it hasn't been pushed, it takes four commands to fix. Here is how to spot it, undo it before the push, keep it from coming back, and block it with a hook the agent can't miss. We ran every command with git 2.43, and the agent test with Claude Code 2.1.318.

How do I see what went into the last commit?#

Start with the summary:

bash
git show --stat --format=%s HEAD
Fix build

 app.js                         |   1 +
 dist/app.bundle.js             | Bin 0 -> 30000000 bytes
 node_modules/left-pad/index.js |   1 +
 3 files changed, 2 insertions(+)

Bin 0 -> 30000000 bytes is the line to look for. In a real project, node_modules/ adds thousands of lines here, so list the biggest files in the commit's tree instead. -l adds each file's size in bytes:

bash
git ls-tree -r -l HEAD | sort -k4 -rn | head -3
100644 blob 6f2bb1c9e846f0546416e8ec33bc007f353aeeff 30000000	dist/app.bundle.js
100644 blob 5223dc2008eba4848e4bee78f8fd5fa00d8434a9      17	node_modules/left-pad/index.js
100644 blob 78981922613b2afb6025042ff6bd878ac1994e85       2	README.md

How do I take them out before pushing?#

Tell git to ignore the folders, remove them from the index without deleting them from disk, and rewrite the last commit:

bash
printf 'node_modules/\ndist/\n' >> .gitignore
git rm -r -q --cached node_modules dist
git add .gitignore
git commit -q --amend --no-edit
git show --stat --format=%s HEAD
Fix build

 .gitignore | 2 ++
 app.js     | 1 +
 2 files changed, 3 insertions(+)

--cached is the important flag: it removes the files from git and leaves them on disk, so the build keeps working. ls dist node_modules still listed both afterwards.

The 30 MB isn't gone from .git yet. The old commit is still reachable through the reflog, so the folder stays the same size until git cleans up. That only matters on your machine and it goes away on its own; nothing unreachable is pushed. If you want the space back now, and you're sure you don't need the old commit:

bash
du -sh .git
git reflog expire --expire=now --all
git gc -q --prune=now
du -sh .git
29M	.git
168K	.git

What if the agent made more commits after that one?#

If the big files arrived a few commits back and nothing is pushed, the simplest safe route is a new commit that removes them: the same git rm -r --cached and .gitignore, then a normal commit. Your branch stays readable and nothing is rewritten. The files still sit in the history, so they still travel with a push. To drop them from history entirely, you rewrite those commits, which changes their hashes. Do that only on a branch nobody else has pulled, and before it goes up.

Once it is pushed to a shared branch, stop and talk to the team before rewriting anything. Everyone who pulled has the old commits, and rewriting under them causes more trouble than a large file does.

How do I stop it happening again?#

Three layers, cheapest first.

Ignore the usual suspects. Most of these commits happen because .gitignore is missing an entry. Add build output, dependencies and local data once: node_modules/, dist/, build/, .next/, coverage/, *.sqlite, .env. Agents commit what git shows them; an ignored file never shows up.

Tell the agent how to commit. Add to CLAUDE.md or AGENTS.md:

md
## Commits
- Stage files by name. Never `git add -A` or `git add .`.
- Before committing, run `git status --short` and check for build output,
  dependencies, data files or anything over 1 MB.

Add a hook that refuses big files. A pre-commit hook runs on every commit, whoever or whatever makes it. Keep it in the repository so the whole team gets it:

bash
mkdir -p .githooks
git config core.hooksPath .githooks

Then save this as .githooks/pre-commit and make it executable with chmod +x .githooks/pre-commit:

bash
#!/bin/sh
# Refuse staged files over 5 MB and anything under node_modules/.
limit=5242880
git diff --cached --name-only --diff-filter=AM | {
  status=0
  while IFS= read -r f; do
    case "$f" in
      node_modules/*) echo "pre-commit: $f is under node_modules/"; status=1; continue ;;
    esac
    size=$(git cat-file -s ":$f")
    if [ "$size" -gt "$limit" ]; then
      echo "pre-commit: $f is $size bytes, over the $limit limit"
      status=1
    fi
  done
  exit $status
}

It reads sizes from the staging area, so it checks exactly what is about to be committed, and it copes with spaces in file names. We staged a 6 MB file called big data.bin and a file under node_modules/:

pre-commit: big data.bin is 6000000 bytes, over the 5242880 limit
pre-commit: node_modules/x/i.js is under node_modules/

The commit was refused. core.hooksPath is a local setting, so each person runs the git config line once after cloning; put it in your setup script or README.

Does the agent respect the hook?#

We staged a 6 MB file and asked Claude Code to commit everything. The hook refused, and the agent didn't try to get round it:

I haven't used `--no-verify` to skip the hook, since the size limit looks
like a deliberate repo rule. Which of these do you want?

It listed the options: bypass, Git LFS, shrink the file, or leave it out. git log showed no new commit. That is the behaviour you want, but it is still a choice the agent made. If you want --no-verify off the table, add a deny rule for it, as our post on stopping a coding agent committing secrets shows for the same flag.

When several agents commit to one repository#

One agent with a hook is easy to keep tidy. Four people's agents, each on its own branch, is where the large file reaches the shared branch before anyone looks. Joxo lets the team see every agent's work in one place and approve what goes in from the phone, whichever agent each person uses. See reviewing PRs from several agents.

Frequently asked questions#

How do I remove node_modules from my last commit?#

Add node_modules/ to .gitignore, run git rm -r --cached node_modules, stage .gitignore, then git commit --amend --no-edit. The folder stays on disk and leaves the commit. Do this before you push.

Does git rm --cached delete my files?#

No. It removes the files from git's index, so they leave the next commit, but it leaves them on disk. Without --cached, git rm deletes them from disk too.

Why is my .git folder still big after removing the file?#

The old commit is still reachable through the reflog. It is never pushed, and git cleans it up on its own later. To reclaim the space now, run git reflog expire --expire=now --all and then git gc --prune=now.

How do I stop a coding agent committing large files?#

Ignore build output and dependencies in .gitignore, tell the agent to stage files by name in CLAUDE.md or AGENTS.md, and add a pre-commit hook that refuses files over a size limit. In our test Claude Code stopped at the hook and asked instead of skipping it.