How can I include untracked files when saving work in a Git stash?
Quick answer
Use git stash push -u to stash tracked changes along with untracked files. Git documents that this option also removes those untracked files from the working tree. Afterward, inspect the entry with git stash show --include-untracked. A plain stash does not include untracked files, and the inspection command needs its include-untracked option to display them. This does not include ignored files.
The question, the details, the next step.
A practical checklist
List the tracked edits and untracked files you intend to save.
Decide whether ignored files are out of scope;
-udoes not include them.In a disposable local repository, run
git stash push -ufor tracked changes plus untracked files.Inspect with
git stash show --include-untrackedand compare the output with your intended file list.Treat
--allas a broader choice because it includes ignored files as well as untracked files.
A few more useful answers
Why doesn’t plain `git stash` pick up a new file?
A plain stash is equivalent to git stash push; the documented -u or --include-untracked option is what adds untracked files. [S1]
Why use `--include-untracked` when showing a stash?
Without that option, the show command may not display untracked contents. Use git stash show --include-untracked when checking coverage. [S1]
Does `git stash push -u` include ignored files?
No. Git documents --all as including ignored files as well as untracked ones; that is broader than -u. [S1]
Sources
References used for this guide.
- [S1] Git Reference: git-stash git-scm.com · Checked
