git stash push, git stash list, git stash apply, git stash pop
git stash push [-q] [-m <message>]
git stash list
git stash apply [-q] [<stash>]
git stash pop [-q] [<stash>]Save dirty tracked files, reapply them, and drop the entry only after a successful pop.
| Parameter | Description |
|---|---|
-m <message> | A description stored with the stash. The list line then reads On <branch>: <message>. |
-q / --quiet | Reduce progress output. apply and pop still print conflicts when they occur. |
Returns: push records a stash and cleans tracked dirty files. apply restores them and keeps the list entry. pop restores them and drops the entry when the apply succeeds.
- The example calls restore --worktree after apply only in this temporary repository, so the following pop can run on a clean file. That restore discards the applied worktree edit.
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'base\n' > f.txt
git add f.txt
git commit -m "Base" >/dev/null
printf 'edit\n' > f.txt
git stash push -q -m "sample stash"
echo "after-push"
st=$(git status --porcelain=v1)
if [ -z "$st" ]; then echo "(clean)"; else printf "%s\n" "$st"; fi
git stash list
git stash apply -q
echo "after-apply"
git status --porcelain=v1
git stash list
git restore --worktree -- f.txt
git stash pop -q
echo "after-pop"
git status --porcelain=v1
st=$(git stash list)
if [ -z "$st" ]; then echo "(empty-list)"; else printf "%s\n" "$st"; fi
Expected output
after-push
(clean)
stash@{0}: On main: sample stash
after-apply
M f.txt
stash@{0}: On main: sample stash
after-pop
M f.txt
(empty-list)
Related: git status, git restore