Stash changes from selected files in Git
Save changes from selected Git paths, include new files when needed, preserve other work, and choose individual hunks interactively.
Use git stash push -- PATH to stash changes for a selected tracked file. The -- separates command options from paths. Run these examples from the repository root and replace the paths with your own.
git stash push -- src/config.js
git stash push -m "Configuration changes" -- src/config.js
git stash push -- a.js b.js dir/
These are alternatives, not a sequence to run on the same change. With the default options, Git rolls the selected paths back to HEAD after saving their changes; modifications outside those paths stay in place.
Inspect the saved changes before restoring them
git stash list
git stash show -p 'stash@{0}'
git stash apply 'stash@{0}'
apply keeps the entry. git stash pop removes it after a successful application, while conflicts leave it in the list. A path-limited stash does not necessarily leave your entire working tree clean, so inspect unrelated work before applying the entry elsewhere.
Include a new file
Untracked files are excluded by default. Add -u to save a selected new file:
git stash push -u -m "New configuration" -- src/new-config.js
git stash show -p --include-untracked 'stash@{0}'
An ignored file is different from an ordinary untracked file. Saving ignored files requires -a rather than -u; include only the paths you intend to save. In particular, avoid sweeping local secrets or generated output into a stash unintentionally.
Select individual hunks
git stash push -p -- src/config.js
Patch mode asks which changes to save. It can separate edits within one file, although changes in the same hunk may need further splitting. Patch mode implies --keep-index unless you override it, so already staged changes remain staged. Read the prompts and inspect git status afterward instead of assuming patch mode has the same index behavior as an ordinary path-limited push.
FAQ
Are staged changes included?
Yes, ordinary stash push handles both staged and unstaged changes for the selected paths. To attempt to restore the original staged state, use:
git stash apply --index 'stash@{0}'
This can fail when the target index or working files conflict with the saved state. Without --index, do not rely on the original staging split being restored.
Do other staged files affect the stash?
A pathspec limits which working changes are saved and rolled back, but a stash also records an index snapshot. Unrelated staged changes can consequently appear in stash inspection or affect later restoration, particularly with --index. If you need a portable change containing exactly selected paths, start with a clean index or export a carefully reviewed patch instead. Do not use a path-limited stash as proof that unrelated staged content is absent.
What if a path contains spaces or wildcard characters?
Quote spaces to keep the path in one shell argument:
git stash push -- 'src/my file.js'
Quotes do not disable Git's own pathspec matching. To address a literal filename that contains characters such as [ or *, use a literal pathspec:
git stash push -- ':(literal)src/config[old].js'
See git-stash and Git pathspec syntax for the command and matching rules.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan