Git Stash: set aside uncommitted work
git stash saves tracked changes on a stack and restores a clean working tree. git stash pop reapplies the newest entry and drops it.
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-59-A: stash cleans the tracked working tree and pop restores the change
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 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' >> f.txtCommand:
git stash >/dev/null; echo clean=$(git status --porcelain | wc -l); echo list_before=$(git stash list | wc -l); git stash pop >/dev/null; echo list_after=$(git stash list | wc -l); echo restored=$(git status --porcelain | wc -l); cat f.txtRecorded output:
clean=0
list_before=1
list_after=0
restored=1
one
twoExit status: 0. The output matches the expected text written before the run.
TE-59-B: git stash push -m saves the change under a message that git stash list shows
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 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' >> f.txtCommand:
git stash push -q -m wip; git stash listRecorded output:
stash@{0}: On main: wipExit status: 0. This status and the output match what was written before the run.
TE-59-C: expected non-zero exit: git stash pop with an empty stash list stops with exit status 1
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 'one\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git stash popRecorded output: none, standard output was empty.
Standard error from the same run:
No stash entries found.Exit status: 1. This status and the output match what was written before the run.
TE-59-B: by the setup, f.txt has an unstaged change. git stash push -m wip saved it, and git stash list printed one entry, stash@{0}: On main: wip. TE-59-C: by the setup, nothing was stashed. git stash pop exited with status 1 and printed No stash entries found. to standard error.
Limits
TE-59-A and TE-59-B cover an unstaged change to a tracked file, and TE-59-C covers an empty stash. Untracked files, staged changes and stash conflicts on pop were not tested. Not tested on this page: the subcommands show, drop, apply, branch and clear, the --index option of pop, and the options of push: -p or --patch, -k or --keep-index, -u or --include-untracked, -a or --all, and --pathspec-from-file with --pathspec-file-nul. The usage text printed by git stash -h in git 2.34.1 lists them. No git stash command here used them.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-59-A | stash cleans the tracked working tree and pop restores the change | 0 | PASS | 77a379f0397b | | TE-59-B | git stash push -m saves the change under a message that git stash list shows | 0 | PASS | 6cf540b5aec0 | | TE-59-C | expected non-zero exit: git stash pop with an empty stash list stops with exit status 1 | 1 | PASS | b014757852b8 |
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