Writing

How to Back Up a Project When AI Agents Edit Your Files

By Scott Wilder · November 8, 2026 · 6 min read · All writing

Commit before every request you send an AI coding agent, and copy the files git ignores (like .env) into a dated folder. Then a bad edit costs you one command, not an hour of retyping. Below is a routine you can paste into a terminal today, and a rule for when git alone is not enough.

Commit before every request, not once per session

One snapshot at the start of a session is better than none. One snapshot before each request is much better.

The reason is the diff. If you prompt the agent while your own edits are uncommitted, git diff afterwards shows your changes and the agent's mixed together. You cannot separate them for a safe revert, so undoing the agent also undoes your work. The suggested order is: make sure the app works, commit your work with a clear message, send the prompt, then commit the agent's changes separately.

The math is simple. Snapshots per session equals the number of requests you send. Send 6 requests and you have 6 safe points to return to. You step back one request at a time instead of losing the whole hour.

The routine: six steps for every request

Example: say you are adding form validation to a signup page, and you already built the form by hand.

  1. Check the starting point works. Run your tests, or open the app and click the main path. If you do not know it worked before, you cannot tell whether the agent broke it. (How to tell if an agent actually finished the task covers the click-through.)
  2. Read git status before you commit. Agents sometimes create config or credential files, and a commit puts a secret into your history for good. If something in the list should not be there, delete it or add it to .gitignore first.
  3. Commit your own work under its own label.
git add -A
git commit -m "Manual: add signup form"
  1. Send one request to the agent. One feature per request. If a request touches two features, split it into two requests so each can be undone alone.
  2. Look at what changed.
git status
git diff

Because step 3 isolated your work, everything you see is the agent's.

  1. Keep it or throw it away.
# good: commit it as its own step, with the agent named
git add -A
git commit -m "Agent: add form validation"

# bad: go back to your snapshot
git restore --source=HEAD .

That last restore overwrites every uncommitted change to tracked files, yours and the agent's. That is exactly why step 3 comes first. Run git status before you use it.

Name the agent in the commit message. Later, git log --oneline shows you which commits came from the agent, which matters when you need to roll back after a bad session.

A commit does not protect the files git ignores

This is where most people get burned. Sessions often start with uncommitted work, untracked files or ignored local files, and a plain commit does not protect that last group. Check what git is ignoring before you trust it:

git status --ignored

Everything under "Ignored files" is outside your snapshot. Typical items: .env, a local database, uploaded images, build output you do not want to regenerate.

What is in your folder Does the snapshot commit save it? What to do
Tracked code you committed Yes Nothing, restore with git restore --source=HEAD .
New files you made, then ran git add -A Yes Nothing
.env, keys, local config in .gitignore No Copy to your dated backup folder
Local database, uploads No Copy to your dated backup folder
Files the agent creates during the session Not yet See the next section

If your .env holds live keys, read how to keep API keys safe with AI coding agents before the next session. A backup copy of that file should sit outside the project folder, not inside a folder the agent can read.

The backup folder is just a copy with a date in the name:

myapp-2026-11-08-1830

Newest name sorts last, so you always know which one to grab. Keep the last 3 and delete older ones.

New files the agent made will stay behind after a restore

git restore puts your tracked files back. It does not remove files the agent created. Those can stay in the folder and break the app in ways that look unrelated.

Because you committed your own work in step 3, everything that is untracked now was made by the agent. See it first, with a dry run that deletes nothing:

git clean -n

Read the list. If every file on it is something you do not want, then run:

git clean -fd

Never run git clean -fd without the dry run. It deletes untracked files permanently, including any new work of yours that you never committed. It also leaves ignored files alone, which is one more reason the backup folder matters.

Work on a throwaway branch when you have half-finished work

Sometimes you have messy, half-done work and you do not want a commit on your main branch. Make the snapshot on a branch you can delete:

git switch -c agent-session
git add -A
git commit -m "Snapshot before agent session"

If the session goes fine:

git switch main
git merge agent-session

If it goes badly:

git switch main
git branch -D agent-session

These are standard git commands, but run the whole sequence once on a throwaway folder before you rely on it with real work.

Give each agent its own folder

Two agents editing one folder is the fastest way to lose track of who changed what. Their edits mix, and neither can be rolled back cleanly. Worktrees suit parallel or experimental agent sessions, because each agent gets its own folder and branch:

git worktree add ../myapp-agent2 -b agent2

If you run several agents regularly, how to manage multiple AI agents without losing track is the next thing to read.

Pick your method with this table

If this is true Do this
The folder is a git repo Commit before each request, commit the agent's work after
You have half-finished work you cannot commit yet Snapshot on a throwaway branch (agent-session)
The folder holds .env, a database or uploads Also copy those into a dated backup folder
You run two agents or two experiments One worktree folder per agent
It is not a git repo and you will not learn git Copy the whole folder before each session, keep the last 3
You want one-command undo and no git commands Use a snapshot tool

On that last row: tools exist that snapshot before the agent edits. unedit snapshots your working tree and steps back with one command, with no git required. Stepback offers a one-command undo and can also rewind the agent's saved chat transcript so the agent resumes from that point. TimeAgent is built around undoing agent changes without losing work you had before the agent started. Pick one only if you will actually use it. A routine you skip is worse than five git commands you run every time.

Four mistakes and what each one costs

Mistake What it costs you
Prompting with your own edits uncommitted The diff mixes both sets of changes, so a revert wipes your work too
Stacking several requests before committing One bad change hides inside several good ones, so you undo all of them or hunt through the diff by hand
Trusting a commit to cover .env and local data After a bad session those files stay changed or gone, and you rebuild keys and data by hand
Committing agent changes under your own message You cannot tell which commits to roll back later

Another habit worth stealing: make the snapshot commit step one of every session, before the agent opens anything. The one-hour after-work session checklist is a good place to bolt it on.

Do this in the next ten minutes

That whole loop takes about ten minutes the first time and under a minute after that.

Still doing this by hand every session?

If you keep repeating a manual routine like this one and think it should already be automated, tell me what it is. Email me with the task and I will tell you whether it is worth automating.

Questions

Do I need git to back up a project before an AI agent edits it?

No. Copy the whole folder to a dated backup before each session and keep the last 3 copies. Git is better because it lets you step back one request at a time, but a folder copy is a real backup.

Does a git commit save my .env file and local database?

Not if they are in your .gitignore. A plain commit does not protect ignored files, so run git status --ignored and copy anything listed under Ignored files into a backup folder.

How often should I commit when an AI agent is editing my code?

Before every request you send, and again after you check the result and decide to keep it. A session with 6 requests then gives you 6 points to return to.

How do I see only what the agent changed?

Commit your own work first under its own message, send the request, then run git diff. Everything it shows is the agent's change.

Can I run two agents on the same project safely?

Not in the same folder. Use git worktree to give each agent its own folder and branch, for example git worktree add ../myapp-agent2 -b agent2, so one cannot overwrite the other's files.