Git Stash Include Untracked: set aside new files too
git stash leaves untracked files in place. git stash --include-untracked also stashes them and removes them from the working tree. git stash pop brings them back.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-99-A: plain stash keeps an untracked file, --include-untracked removes it and pop restores it
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'a2\n' >> f.txt
printf 'n\n' > new.txtCommand:
git stash -q; echo plain_new_exists=$([ -f new.txt ] && echo yes || echo no); git stash pop -q; git stash -q --include-untracked; echo with_u_new_exists=$([ -f new.txt ] && echo yes || echo no); git stash pop -q; echo after_pop_new_exists=$([ -f new.txt ] && echo yes || echo no)Recorded output:
plain_new_exists=yes
with_u_new_exists=no
after_pop_new_exists=yesExit status: 0. The output matches the expected text written before the run.
TE-99-B: after stash --include-untracked the working tree is clean and one stash entry exists
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'n\n' > new.txtCommand:
git stash -q -u; echo clean=$(git status --porcelain | wc -l); git stash list | wc -lRecorded output:
clean=0
1Exit status: 0. This status and the output match what was written before the run. The status is that of wc, the last command in the pipe.
TE-99-C: expected non-zero exit: git stash with an option it does not have prints an error and exits with status 129
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git stash --nosuchoptionRecorded output: none, standard output was empty.
Standard error from the same run:
error: unknown option `nosuchoption'
usage: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--] [<pathspec>...]]
-k, --keep-index keep index
-p, --patch stash in patch mode
-q, --quiet quiet mode
-u, --include-untracked
include untracked files in stash
-a, --all include ignore files
-m, --message <message>
stash message
--pathspec-from-file <file>
read pathspec from file
--pathspec-file-nul with --pathspec-from-file, pathspec elements are separated with NUL characterExit status: 129. This status and the output match what was written before the run.
TE-99-B: by the setup, new.txt is untracked. After git stash -q -u, git status --porcelain printed no line, so clean=0, and git stash list printed one entry. TE-99-C: git stash --nosuchoption exited with status 129. The first line of standard error is error: unknown option `nosuchoption' and it is followed by the usage text.
Limits
Ignored files are not covered (--all handles those, not tested). The run uses one untracked file and no conflicts.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-99-A | plain stash keeps an untracked file, --include-untracked removes it and pop restores it | 0 | PASS | 4b5f9ae63a49 | | TE-99-B | after stash --include-untracked the working tree is clean and one stash entry exists | 0 | PASS | d6390aba88e1 | | TE-99-C | expected non-zero exit: git stash with an option it does not have prints an error and exits with status 129 | 129 | PASS | 3eba38c43a60 |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides