PricingSign inDownload

Blog / git

Clean up the stray files a coding agent left behind

Debug logs, one-off scripts and .orig files after an agent run? List them with git, keep a copy, delete only what you mean to, and keep .env safe.

On this page
  1. List what the agent left
  2. Preview first, always
  3. Delete only what you mean to
  4. Keep a copy first
  5. Don't wipe ignored files by accident
  6. The tracked files it changed
  7. Give the agent a scratch folder
  8. Frequently asked questions
  9. How do I see untracked files inside new folders in git?
  10. Does git clean delete my .env file?
  11. How do I delete only some untracked files?
  12. Can I undo git clean?
  13. Try it with your team

A coding agent tries things. It writes a script to fix imports, a quick test in a scratch folder, a debug.log, and an app.js.orig left over from a merge. When the run ends, your project has files nobody meant to keep:

 M app.js
?? app.js.orig
?? debug.log
?? fix_imports.sh
?? scratch/

Lines with ?? are files git doesn't track. Some of them may matter, like a new source file the agent forgot to add. The rest should go. Here is how to tell them apart and remove the junk without touching your real work or your .env. We ran every command with git 2.43 on a scratch repository.

List what the agent left#

git status --short folds a new folder into one line. To see every file inside it:

bash
git status --short --untracked-files=all
 M app.js
?? app.js.orig
?? debug.log
?? fix_imports.sh
?? scratch/test_run.py

For a plain list you can feed to other commands, git has one that leaves out ignored files such as node_modules and .env:

bash
git ls-files --others --exclude-standard

Read the list before deleting anything. A new file the agent wrote for the feature is untracked too, until someone runs git add. If you see one, add it now so the steps below leave it alone.

Preview first, always#

git clean deletes untracked files, and it doesn't put them in the trash. Always run it with -n first, which only prints what it would do:

bash
git clean -n
Would remove app.js.orig
Would remove debug.log
Would remove fix_imports.sh

Plain git clean skips folders. Add -d to include them:

bash
git clean -nd
Would remove app.js.orig
Would remove debug.log
Would remove fix_imports.sh
Would remove scratch/

Notice what isn't on either list: .env and node_modules/. Both are in .gitignore, and git clean leaves ignored files alone unless you ask.

Delete only what you mean to#

Name the files and folders you want gone, after --:

bash
git clean -fd -- scratch debug.log
Removing debug.log
Removing scratch/

The other untracked files stayed. To remove everything on the preview list except one file, exclude it with -e:

bash
git clean -nd -e fix_imports.sh
Would remove app.js.orig
Would remove debug.log
Would remove scratch/

When the preview is right, run the same command with -f in place of -n. Git refuses to clean without -f by default, so a typo can't delete anything.

Keep a copy first#

If you're not sure about some of it, copy every untracked file somewhere outside the project before you clean, keeping their folders:

bash
mkdir -p ../leftovers
git ls-files --others --exclude-standard -z | xargs -0 cp --parents -t ../leftovers

In our test this copied notes.txt and scratch/run.py into ../leftovers, with the scratch folder kept. Then clean as usual and delete the copy when you're sure. The -z and -0 keep file names with spaces working. cp --parents needs GNU cp, which Linux has; the Mac's own cp doesn't have it, so there copy the few files you want to keep by hand.

Don't wipe ignored files by accident#

Two flags reach into ignored files, and they're the ones that hurt:

bash
git clean -ndX
Would remove .env
Would remove node_modules/

-X removes only ignored files. -x removes ignored and untracked files together:

bash
git clean -ndx
Would remove .env
Would remove app.js.orig
Would remove debug.log
Would remove fix_imports.sh
Would remove node_modules/
Would remove scratch/

Both would delete your .env, which holds keys that aren't in git and may not be anywhere else. -x is fine for a fresh clone or a CI job; on your own copy of a project, leave it alone. If you share a repository between several worktrees, each has its own untracked and ignored files; see env files and node_modules in git worktrees.

The tracked files it changed#

git clean never touches tracked files, so app.js above is still modified. That one is the agent's actual work, or part of it. Review it with git diff and keep, edit or restore it as you would any change. For a quick look at a whole run, git diff --stat and git status --short together show everything the agent touched.

Give the agent a scratch folder#

Agents leave files around because they have nowhere better to put them. Give them one. Add a folder to .gitignore:

tmp/

Then add a line to your project instructions: "Put scratch files, test scripts and logs in tmp/, and delete them when you're done. Don't leave new files outside tmp/ unless they're part of the change." Now the leftovers stay out of git status, and cleaning up is rm -rf tmp.

A few other habits help:

  • Ask the agent to end each task with git status --short and to explain any untracked file it lists. That turns forgotten files into a question before they become clutter.
  • Commit before a long run. If the agent leaves a mess, you know exactly which files are new.
  • Watch for processes as well as files. Dev servers and watchers left running are the other common leftover; see a coding agent left a dev server running.

Frequently asked questions#

How do I see untracked files inside new folders in git?#

Run git status --short --untracked-files=all. Without the flag, git shows a new folder as one line instead of listing its files.

Does git clean delete my .env file?#

Not with git clean -fd, as long as .env is in .gitignore. The -x and -X flags do delete ignored files, including .env, so preview with -n first.

How do I delete only some untracked files?#

Name them after --, as in git clean -fd -- scratch debug.log, or exclude a file with -e. Run the same command with -n first to check.

Can I undo git clean?#

No. Git clean deletes files that git never stored, so git can't bring them back. Copy them out first, or check your editor's local history and your backups.