Sub topic 2
Stash: shelve work in progress
This chapter assumes you know how to add, commit, create branches, and switch between them. It also assumes you know that the working tree, the index (staging area), and commits are separate places where changes live.
The problem stash solves
You are halfway through a change when an urgent bug report arrives. Your edits are not ready to commit. Git also refuses to switch branches if the switch would overwrite your uncommitted changes. You need somewhere to put the work and get a clean working tree.
git stash saves your uncommitted changes (both staged and unstaged edits to tracked files) and reverts the working tree to match HEAD. You can bring the changes back later, on any branch.
The basic cycle
git stash push -m "wip: login form validation"
git stash list
git switch hotfix-branch
# ...make and commit the urgent fix...
git switch -
git stash popStep by step:
git stash push -m "..."records your changes and cleans the working tree. The message is optional, but it makes the stash easy to recognize later.git stash listshows every saved stash, newest first, asstash@{0},stash@{1}, and so on.- You switch branches, do the urgent work, and commit it.
git switch -returns you to the previous branch.git stash popreapplies the newest stash and removes it from the list.
Inspecting a stash
Before you restore anything, you can look at what a stash contains.
git stash listprints one line per stash with its message.git stash showprints a summary of files changed in the newest stash.git stash show -p stash@{1}prints the full patch for a specific stash.
apply, pop, and drop
The three commands that finish a stash differ in one respect: whether the entry stays in the list.
| Command | Reapplies changes | Removes the stash entry |
|---|---|---|
| `git stash apply [stash]` | Yes | No |
| `git stash pop [stash]` | Yes | Only if it applies cleanly |
| `git stash drop [stash]` | No | Yes |
Use apply when you want to keep a copy, for example to apply the same changes to several branches. Use pop for the usual case of shelving and restoring once. Use drop to delete a stash you no longer need.
If no stash is named, Git uses stash@{0}.
Stashing and the index
By default, pop and apply restore your edits as unstaged changes, even if you had staged them. To restore the staged state too, add --index:
git stash pop --indexThis can fail with a conflict if the working tree has changed since the stash was made, so use it only when the staging distinction matters.
A common mistake: forgetting new files
A plain git stash push ignores untracked files. If your work-in-progress includes a brand new file, it stays in the working tree after stashing. Chapter material on options covers -u for this case. For now, run git status after stashing to confirm the tree is actually clean.
What to expect when restoring
You do not have to return to the same branch. A stash is not tied to the branch where you created it. Popping onto a different branch works as long as the changes apply without conflict. This makes stash handy for moving unfinished work to the correct branch after you realize you started on the wrong one.
Exercise
You run git stash pop and Git reports a merge conflict. What happens to the stash entry, and what should you do next?
Show answer
Git keeps the stash entry because the pop did not apply cleanly. Resolve the conflicted files, stage them if needed, confirm the result is what you want, and then run git stash drop to delete the entry.
Exercise
You have two stashes and want to see the patch of the older one without applying it. Which command do you run?
Show answer
git stash show -p stash@{1}. The newest stash is stash@{0}, so the older one is stash@{1}.