git stash for AI agents: park uncommitted work, inspect it, and restore it without losing anything
TL;DR: git stash push -u moves your tracked and untracked changes out of the working tree and into a commit under refs/stash, which is how you hand a clean directory to an agent without deleting your work. On Git 2.47.3 I reproduced the whole lifecycle: pushing with -u, reading it back with git stash show -p, restoring it with apply (which keeps the stash) versus pop (which drops it), and the case that bites people - a pop that conflicts exits 1, prints “The stash entry is kept in case you need it again”, leaves the file in UU state, and then makes every later git stash command fail with “a.txt: needs merge” until you resolve it. git stash is a parking lot, not a backup, and it does not park ignored files unless you pass -a.
Everything below was run on git version 2.47.3 in throwaway repositories on November 23, 2026. The outputs are copied verbatim.
The situation this solves
An AI coding agent works on the files it decides to work on. If you have uncommitted edits in those files, two things can happen, and both are bad:
- The agent reads your half-finished file as if it were the intended state and rewrites it around your changes.
- You resolve the conflict by running
git checkout -- .and throw away work that was never committed anywhere.
Neither failure is the agent’s fault. The fix is to make the working tree clean before the agent starts, and to keep your edits recoverable while it runs. git stash is the built-in mechanism for exactly that, and it is the one Git feature that is designed to hold uncommitted state.
The official git-stash documentation opens with the purpose: “Use git stash when you want to record the current state of the working directory and the index, and revert it back to the state of the HEAD.” That is literally a parking lot for work in progress.
What git stash push -u actually does
Starting state: one tracked file with an edit, one untracked file, and one ignored file:
$ git status --short
M a.txt
?? b.txt
Note that c.log does not appear: it is listed in .gitignore.
$ git stash push -u -m "park user edits"
Saved working directory and index state On master: park user edits
$ echo "exit=$?"
exit=0
$ git status --short
$ git stash list
stash@{0}: On master: park user edits
The working tree is now clean and the stash is on the stack. The -u flag (--include-untracked) is what makes the untracked file b.txt part of the stash. Without -u, b.txt would have stayed on disk and the agent could still have overwritten it.
What did not get parked: c.log. Ignored files are not included by -u; they need -a (--all), which also stashes ignored files. This matters for agent workflows - build artifacts, .env files, and model weights that are gitignored are exactly the files an agent is most likely to stomp on, and git stash push -u will not save them.
Reading the stash before you restore it
$ git stash show -p "stash@{0}"
diff --git a/a.txt b/a.txt
index a29bdeb..0f0fa6a 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
line1
+user work in progress
git stash show prints the diff of the stashed changes. Note that the untracked file b.txt does not appear here: untracked content is stored in a separate parent commit inside the stash. If you want to see it, the documentation lists git stash show -u / --include-untracked and --only-untracked as options, so use -u when you are not sure what is in there.
A quick inventory without the diff:
$ git stash show --stat "stash@{0}"
a.txt | 1 +
1 file changed, 1 insertion(+)
apply versus pop - the difference that matters
Both restore the stashed changes into your working tree. The difference is what happens to the stash entry afterwards.
$ git stash apply "stash@{0}"
On branch master
Changes not staged for commit:
modified: a.txt
Untracked files:
b.txt
$ git stash list
stash@{0}: On master: park user edits
apply leaves the stash on the stack. pop restores the same changes and then drops the entry:
$ git stash pop
On branch master
Changes not staged for commit:
modified: a.txt
Untracked files:
b.txt
Dropped refs/stash@{0} (40a6fa77b327fe2c8ab13e5e86d263b6d8bb3573)
$ echo "exit=$?"
exit=0
Rule of thumb: use apply while you are still deciding, pop only when you are sure you want the stash gone. In agent workflows apply is almost always the right choice, because the agent may produce a version of the file you want to compare against the parked original - and if you pop first, the parked copy is gone.
Which command to reach for
| Command | What it does to the tree | What it does to the stash | Use it when |
|---|---|---|---|
git stash push -u | Moves tracked + untracked changes out | Pushes a new entry | You are handing the directory to an agent |
git stash push -m "msg" -- a.txt | Moves only the named paths | Pushes a new entry | The agent touches one directory |
git stash push --keep-index | Leaves the staged part in place | Pushes a new entry | The staged set must stay visible to the agent |
git stash show -p | Nothing | Reads the entry | You want to read the parked diff before deciding |
git stash apply | Restores the changes | Keeps the entry | You may need the parked copy again later |
git stash pop | Restores the changes | Drops the entry (unless the restore conflicts) | You are sure and there is no conflict |
git stash branch feature-x | Restores the changes on a new branch | Drops the entry | The parked work should become real history |
git stash create + git stash store | Nothing, then nothing | Adds an entry later | Scripted snapshots |
The conflict case, reproduced
This is the case that surprises people, because the stash survives but your repository is left in a state where the usual commands start failing.
The agent edited a.txt while your work was parked, so the file has changed on both sides:
$ git stash pop
Saved working directory and index state On master: second stash
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
On branch master
Unmerged paths:
both modified: a.txt
The stash entry is kept in case you need it again.
$ echo "exit=$?"
exit=1
$ git status --short
UU a.txt
$ git stash list
stash@{0}: On master: second stash
Two facts fall out of this output. First, pop exited 1 and the stash was not dropped - Git keeps it precisely so you do not lose the parked copy. Second, the index is now unmerged, and that state blocks the stash machinery:
$ git stash push -m "nothing"
error: could not write index
a.txt: needs merge
$ echo "exit=$?"
exit=1
$ git stash branch feature-parked "stash@{0}"
error: you need to resolve your current index first
a.txt: needs merge
$ echo "exit=$?"
exit=1
So after a conflicted pop you must resolve the conflict (git add a.txt after fixing it, or git checkout --theirs a.txt to take the stashed version wholesale) before you can stash or branch again. If you want the parked version and nothing else, git checkout stash@{0} -- a.txt restores that single file from the stash.
Two flags that change what gets parked
--keep-index parks only the unstaged part and leaves the staged part in place - useful when the agent should keep seeing the files you already staged as a deliberate unit:
$ git status --short
MM a.txt
$ git stash push --keep-index -m "keep index"
Saved working directory and index state On master: keep index
$ git status --short
M a.txt
MM became M : the staged change stayed in the index, the unstaged change went to the stash.
A pathspec parks only the files you name, which is the surgical version for agent runs that touch one directory:
$ git stash push -m "only a.txt" -- a.txt
Saved working directory and index state On master: only a.txt
$ git status --short
M b.txt
$ git stash show --name-only "stash@{0}"
a.txt
The documentation lists the full synopsis - git stash [push] [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet] [-u | --include-untracked] [-a | --all] [(-m | --message) <message>] [--pathspec-from-file=<file>] [--] [<pathspec>...] - so a single path is only the simplest case of a pathspec-based park.
Two commands for scripted workflows
git stash create makes a stash commit without touching the working tree and without adding anything to the stash list; git stash store then files it:
$ SHA=$(git stash create "created stash")
$ echo "create sha=$SHA"
create sha=ec331a03fe75a36f1f2c48e805f9c14b062309a2
$ git stash list
stash@{0}: On master: keep index
$ git stash store -m "stored from create" "$SHA"
$ git stash list
stash@{0}: stored from create
stash@{1}: On master: keep index
That pair is what you want in a script: take a snapshot of the dirty tree, do something else with the tree, and file the snapshot only if you decide you need it.
git stash branch turns a stash into a branch, which is the cleanest way to keep a parked change as real history:
$ git stash branch feature-parked "stash@{0}"
Switched to a new branch 'feature-parked'
On branch feature-parked
Changes not staged for commit:
modified: a.txt
Dropped stash@{0} (e9ca373a792849eb00add763daee80d10d11173f)
$ git branch -v
* feature-parked 914171e init
master 914171e init
The change now lives on a branch you can inspect, diff, and delete like any other work.
The limits worth stating
- A stash is not a backup. It is a commit chain under
refs/stash, and it is reachable from the reflog, but nothing about it is pushed anywhere. If the repository is deleted, the parked work goes with it. For anything you care about, commit it on a branch -git stash branchabove is the one-liner. - Ignored files are not parked unless you pass
-a, and-aon a directory with multi-gigabyte artifacts can be slow and pointless. - Stash entries are a stack, not a map.
stash@{0}is the most recent, and the index shifts every time you push or drop something. Label every stash with-m, and if you need a stable handle, usegit stash branchorgit stash create/storeinstead of a rawstash@{N}. - A conflicted pop blocks the index until you resolve it, as shown above.
- Stash does not replace worktrees. For running two agents at once, or keeping the agent’s output next to your own tree, a worktree is the right tool; removing agent worktrees without losing uncommitted work covers that side. Stash is for the sequential case: park, hand over the directory, take the work back.
Honest take
git stash push -u is the cheapest insurance in Git, and the failure it prevents - losing an afternoon of work to git checkout -- . - is exactly the failure an agent makes easy. Park the work, hand over a clean tree, and use apply rather than pop until the agent’s result is one you are keeping.
The two rules that keep this safe: always label the stash, and never treat refs/stash as the only copy of work that matters. If the change is worth keeping for more than the length of one agent run, put it on a branch - git stash branch takes one command and turns a temporary parking spot into real history you can diff, push, or throw away.
Sources
- git-stash - official Git documentation for the stash commands and their options
- git-checkout - restoring files and paths from the index, a commit, or a stash
- git-commit - how the stashed changes are recorded as commits
- git-worktree - running several working trees from one repository
- git-reset - what it does to the index, and why a conflicted pop leaves it unmerged
- git-merge - how a stash is applied back as a merge, and where conflicts come from
- Removing agent worktrees without losing uncommitted work
- Finding an AI agent’s regression with git bisect
- Cleaning up an agent’s commit history with fixup and autosquash
Last updated November 23, 2026. Every command and output above was run on Git 2.47.3 on November 23, 2026 in a throwaway repository; the option lists and the stash description come from the official git-stash documentation read the same day. Nothing here was tested against a live AI coding agent - it was tested against Git.
Was this article helpful?
Thanks for the feedback — it helps improve future articles.
Need hands-on help?
I offer 1-on-1 technical consulting for local AI setup, GPU selection, and AI coding tool configuration — same topics covered on this site.
Book a session — $49 / hour →Know which coding tool is worth paying for
Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.