Git Tutorial: Using the Stash Command
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Use `git stash save` to temporarily store uncommitted changes and reset the working tree so branch checkouts aren’t blocked.
Briefing
Git stash is the safety net for uncommitted work: it temporarily saves local changes so developers can switch branches, inspect other code, or back out to a clean working tree—then restore those exact changes later. In practice, it prevents the “uncommitted changes” block that stops branch checkouts, while keeping work from being lost when priorities shift.
The walkthrough starts with a simple Python file whose arithmetic functions initially lack return values. After creating and checking out an `ad` branch, the author edits the `add` function (adding `return a + b`) and confirms the modifications with `git diff`. When the need arises to return to `master` and see the file as it was before the edits, the changes are stashed using `git stash save` along with a message like “worked on ADD function.” Immediately after stashing, `git diff` shows no pending differences and `git status` reports nothing to commit; the working directory is reverted to the pre-stash state.
Those saved edits remain available in the stash list. Running `git stash list` shows stash entries with numeric IDs (e.g., `stash@{0}`) and the custom message used at save time. To bring changes back, two commands appear: `git stash apply` and `git stash pop`. `git stash apply` restores the stashed changes but leaves the stash entry in place, meaning it can be reapplied later. `git stash pop` restores the top stash and removes it from the stash list in one step, leaving the stash history cleaner once the work has been recovered.
The tutorial then demonstrates stacking multiple stashes. After adding more functions (like `subtract`, `multiply`, `divide`, and later a `square` function), each set of edits is stashed with its own message. As new stashes are created, older ones shift down the list: the most recent change sits at the top (`stash@{0}`). Using `git stash pop` repeatedly pulls off the latest stash first, restoring changes in reverse chronological order.
For cleanup, `git stash drop` deletes a specific stash entry by ID, while `git stash clear` removes all stashes at once. The examples emphasize caution with `git stash clear`, since it permanently discards every saved stash and leaves no way to recover those uncommitted changes.
Finally, the practical branch-switching scenario ties everything together. On `master`, uncommitted edits prevent checking out the `ad` branch. Stashing the changes on `master` clears the working tree, enabling `git checkout ad`. After switching, `git stash pop` reintroduces the exact edits that were meant for the `ad` branch, after which the changes can be staged and committed normally.
Cornell Notes
Git stash lets developers save uncommitted changes temporarily, switch branches, and restore work later. After editing on a branch, `git stash save` records the current working directory state and resets the tree so `git status` shows no pending commits. `git stash apply` restores changes without removing the stash, while `git stash pop` restores the top stash and deletes it from the stash list. Multiple stashes can be stacked, with the newest at `stash@{0}`, and they can be removed selectively using `git stash drop` or all at once with `git stash clear`. This workflow prevents branch checkout errors caused by uncommitted changes and keeps work from being lost.
What problem does `git stash` solve when switching branches?
How do `git stash apply` and `git stash pop` differ in practice?
Why does stash order matter when multiple stashes exist?
When should `git stash drop` or `git stash clear` be used?
How does stash help in the “wrong branch” workflow?
Review Questions
- What does `git stash save` do to the working directory, and how can you verify it with `git diff` and `git status`?
- If you run `git stash apply` and then `git stash list`, what should you expect to see about the stash entry?
- How would you restore the second-most-recent stash if you have multiple stashes—what command would you use and how would you identify the correct stash ID?
Key Points
- 1
Use `git stash save` to temporarily store uncommitted changes and reset the working tree so branch checkouts aren’t blocked.
- 2
Stashes persist until explicitly restored or removed; `git stash list` shows stash IDs and the custom messages used at save time.
- 3
`git stash apply` restores changes without deleting the stash, while `git stash pop` restores and removes the top stash entry.
- 4
When multiple stashes exist, `stash@{0}` is the newest; popping restores changes in reverse order of creation.
- 5
Use `git stash drop <stash-id>` to delete a single stash, and `git stash clear` to delete all stashes (destructive).
- 6
Stash is especially useful when edits were made on the wrong branch: stash on the current branch, checkout the intended branch, then pop/apply to continue work.