Skip to content

/flags/git/git-stash-u-include-untracked

git stash -u (--include-untracked): stash new files too

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated
tool
Git
command
git stash
flag
-u, --include-untracked
default
off (only tracked changes and staged files are stashed)

git stash -u (--include-untracked) saves your modified tracked files and your untracked files in the stash, then cleans them out of the working tree. Without -u, git stash only stashes changes to files Git already tracks (plus anything staged), so a brand-new file you never ran git add on stays behind. That surprises people who stash to get a clean tree before a checkout, a pull or a rebase.

Without and with -u

bash
git init -q -b main repo && cd repo
echo one > tracked.txt && git add tracked.txt && git commit -qm init
echo change >> tracked.txt
echo new > notes.txt
echo "== git stash"
git stash -q
git status --short
git stash pop -q
echo "== git stash -u"
git stash -q -u
git status --short
echo "(clean)"
git stash show --include-untracked --name-status
git stash pop -q
git status --short

Output

== git stash
?? notes.txt
== git stash -u
(clean)
A	notes.txt
M	tracked.txt
 M tracked.txt
?? notes.txt

Plain git stash took the edit to tracked.txt and left notes.txt (shown as ??). With -u both are saved and the tree is clean; git stash pop puts both back. The untracked files are stored in a third commit attached to the stash entry, which is why git stash show lists them only with --include-untracked.

CommandWhat goes into the stash
git stashModified tracked files and staged changes (including staged new files)
git stash -u, --include-untrackedThe above plus untracked files that are not ignored
git stash -a, --allThe above plus ignored files (build output, dependencies, local env files)
git stash -k, --keep-indexStash everything, but keep the staged changes in the working tree and index
git stash --stagedOnly the staged changes
git stash push -u -- PATHLimit the stash to the given paths, untracked ones included
git stash -u -m "msg"Same as -u with a description shown by git stash list

git stash save is the older, deprecated spelling; git stash push (or plain git stash) accepts the same -u and -a options.

When you need it

  • Switching branches. A checkout that would overwrite an untracked file aborts with "untracked working tree files would be overwritten by checkout". Stashing with -u moves the file out of the way; see also local changes would be overwritten by checkout for tracked files.
  • Pulling with rebase. git pull --rebase refuses to run on a dirty tree (cannot pull with rebase); git stash -u, pull, then git stash pop.
  • Testing a clean tree. Before running a build to check that a commit is complete, -u removes stray new files that might make the build pass by accident.

Pitfalls

  • Pop conflicts with new files. If a file with the same name as a stashed untracked file exists when you run git stash pop, Git refuses to restore it and keeps the stash entry (see the block below). Move or delete the file and pop again.
  • -a is heavy. --all stashes node_modules, virtual environments and build directories if they are ignored, then deletes them from disk. Prefer -u unless you really want ignored files too.
bash
git init -q -b main repo && cd repo
echo one > tracked.txt && git add tracked.txt && git commit -qm init
echo new > notes.txt
git stash -q -u
echo "recreated" > notes.txt
git stash pop

Output · exit 1

Already up to date.
notes.txt already exists, no checkout
error: could not restore untracked files from stash
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

nothing added to commit but untracked files present (use "git add" to track)
The stash entry is kept in case you need it again.

The pop fails with exit code 1, notes.txt keeps the recreated content and stash@{0} is still there.

FAQ

Why did git stash leave my new file behind?

Plain git stash saves only tracked files: changes to files Git already knows and staged new files. A file that was never added is untracked, so it stays in the working tree. Use git stash -u to include it.

What is the difference between git stash -u and git stash -a?

-u adds untracked files that are not ignored. -a (--all) also adds ignored files, such as build output, node_modules or .env files listed in .gitignore, which can make the stash very large.

How do I see the untracked files inside a stash?

git stash show --include-untracked lists them with the other changes, and git stash show -p --include-untracked prints their contents as a diff.

Sources