Git Stash: Save and Inspect Temporary Changes
Pause without making a feature commit
Imagine you are writing plant notes when an urgent repair arrives. You have staged one edit, left another unstaged and created a new draft file. A stash stores selected unfinished work in the local repository so you can return to it later. It does not advance your current branch or upload anything. A stash is not an independent backup: losing the repository can lose the stash too.
Know what will be saved
Ordinary git stash push saves staged and unstaged tracked changes, including staged new paths. Untracked files remain unless you use -u. Ignored files remain with -u; -a also includes ignored files and may capture large build output or sensitive local data. Do not use -a casually. Review status and diffs before saving. In a disposable repository with at least one commit, create a tracked notes.txt and light.txt and a .gitignore containing *.cache, then commit them. The commands below stage notes, leave light unstaged and create draft.txt and local.cache.
printf "Water every 2 days\n" > notes.txtgit add -- notes.txtprintf "Indirect light\n" > light.txtprintf "Draft\n" > draft.txtprintf "Cache\n" > local.cachegit status --short --ignoredgit diffgit diff --cachedSave with a descriptive message
Use -u in this fixture because draft.txt is worth saving and local.cache is disposable. After the command, both tracked files match HEAD, draft.txt is absent and local.cache remains. git status --short --ignored reports !! local.cache; ordinary short status is empty. The commit ID of HEAD is unchanged. The stash list is shared across worktrees and the numeric selector is relative to the newest entry.
git stash push -u -m "Watering and light draft"git stash listgit stash show -p -u "stash@{0}"git status --short --ignoredPractice and check
Predict whether a new untracked file would remain after default push. It remains. Predict whether the ignored cache is included by -u. It is not. Inspect the patch, including untracked content with show -u, before trying to restore it. Avoid putting secrets in a stash. Use the chapter lab to compare default, -u and -a in independent disposable repositories.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Stash: Save and Inspect Temporary Changes — knowledge check
Reason about temporary Git work and recovery.
Which changes are included by default?
Which option also includes non-ignored untracked files?
What additional files can -a capture?
What happens to the current branch tip after stash push?
How do you inspect a saved patch including untracked content?