Before this lesson
Understand uncommitted changes, branch switching and conflicts.
Use Git 2.35+ and a Bash-compatible terminal, such as Git Bash on Windows. Each complete example creates an independent temporary directory. Run it only in that practice repository. Configuration is repository-local and uses a practice identity. Temporary directories are not automatically removed; clean them up after finishing.
What you will be able to do
Save unfinished work and remove the saved entry only after checking restoration.
Understand the mechanism
stash temporarily shelves worktree and index changes while switching tasks. It is local and does not travel with normal pushes like branch commits. Use descriptive branches and commits for longer-lived work.
Default stash excludes untracked files; -u includes them but still excludes ignored files. Use apply --index deliberately when restoring the original staged distinction matters.
apply restores while retaining the entry; pop attempts to remove it after successful restoration. This lesson uses apply, inspect and drop so cleanup follows verification. Restoration can also conflict.
Runnable example
lab=$(mktemp -d)
cd "$lab"
git init -b main .
git config user.name "Honeysuck Learner"
git config user.email 'learner'@'example.invalid'
git config commit.gpgsign false
git config core.autocrlf false
printf 'Start learning\n' > notes.txt
git add notes.txt
git commit -m "Start learning notes"
printf 'Unfinished chapter\n' >> notes.txt
printf 'Outline\n' > outline.txt
git stash push -u -m "Draft chapter and outline"
git stash list
git status --short
git stash show -u --stat 'stash@{0}'
git stash apply 'stash@{0}'
cat notes.txt
cat outline.txt
# Verify both files above before removing the saved entry.
git stash drop 'stash@{0}'
git status --short
Trace and verify
Saving leaves a clean worktree. Applying restores the notes modification and untracked outline.txt. Dropping removes the saved entry, not the restored working files.
Common mistakes and boundaries
Stash is neither secret storage nor a sole backup. Confirm the entry and restored files before dropping it; indices change when entries are added or removed.
Practice challenge
Rerun without -u and check whether outline.txt remains after stashing.
Reference solution and reasoning
The untracked outline remains; only changes to tracked notes are shelved. This explains why untracked files may still obstruct a switch.