Stash untracked files in Git

Include new files with stash -u, distinguish ignored files from untracked files, and restore a stash without losing track of conflicts.

Best practices
Illustration of Git branches and version history

Use git stash push -u to stash untracked files along with tracked changes. Use -a only when ignored files should be included too. Review the scope first: a large ignored directory can make an all-files stash expensive.

Choose the scope

git status --short
git stash push -u -m "Work in progress"

The command saves tracked index and working-tree changes plus untracked files, then cleans up the saved work from the checkout. Plain git stash push normally omits untracked files.

These are alternative forms:

# Include ignored files as well:
git stash push -a -m "Include local generated files"
# Limit the stash to selected paths:
git stash push -u -- src/new-file.js

A path restriction is not an “untracked only” switch. It includes applicable tracked changes at matching paths too. To save only untracked files, select only the specific untracked paths you intend to save.

Inspect and restore

git stash list
git stash show -p --include-untracked 'stash@{0}'
git stash apply 'stash@{0}'

Plain stash show -p does not necessarily display the untracked part; request it explicitly on a version that supports that option. apply retains the stash entry for later review. pop attempts to apply it and removes the entry only when application succeeds.

Untracked files are normally restored as untracked files. Use --index when you also need Git to attempt restoration of the original staged state; conflicts can prevent that reconstruction.

Understand collisions and storage

If an untracked file now occupies a path the stash needs to restore, application can fail. Preserve the current file elsewhere, inspect any changes already applied, and retry deliberately. Do not repeatedly pop without checking the resulting index, working tree, and stash list.

A stash stores Git objects. Including untracked or ignored files adds the content needed to restore them; large files can substantially increase storage and processing time. Stashing is local and is not an off-machine backup.

Will -u stash node_modules? Only its ignore and tracking status determines that. If the directory is untracked and ignored, -u excludes it; if it is not ignored, its contents may be included. Already tracked files are treated as tracked regardless of ignore patterns.

Can I safely include ignored credentials? Including a file stores its contents in the local object database. Avoid collecting secrets merely because -a is convenient, and use a deliberate backup or secret-storage policy instead.

See git-stash and Git ignore behavior.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting