Git Stash
The git stash command temporarily saves changes that are not ready to be committed, then clears the working directory. The saved changes can be retrieved and reapplied later.
This is incredibly useful when work is in progress, but there is an urgent need to switch branches or fix something else — without losing the current work or making a messy "work in progress" commit.
Real-life analogy: Think of stashing like putting work-in-progress papers in a desk drawer before attending an urgent meeting. The work is paused, the desk is cleared, and when returning from the meeting, the papers can be pulled back out and work can resume exactly where it left off.
Basic Stash Commands
Save Current Changes to the Stash
git stashThis saves all modified and staged files into the stash and reverts the working directory to match the last commit. Untracked files are NOT stashed by default.
Save with a Descriptive Message
git stash save "work in progress: adding login form"Adding a message makes it much easier to identify stashes later.
View All Stashes
git stash listOutput:
stash@{0}: On feature-login: work in progress: adding login form
stash@{1}: On main: quick style fix not ready to commit
stash@{2}: WIP on main: b7c3d45 Update homepage
Each stash is identified by its index (stash@{0} is the most recent).
Apply the Most Recent Stash
git stash applyRestores the changes from the most recent stash but keeps the stash in the list. The stash can be applied again later if needed.
Apply and Remove the Most Recent Stash
git stash popRestores the most recent stash and removes it from the stash list. This is the most commonly used command for retrieving stashed work.
Apply a Specific Stash
git stash apply stash@{2}Remove a Specific Stash
git stash drop stash@{1}Remove All Stashes
git stash clearPractical Example — The Urgent Switch Scenario
# Working on a new feature
echo "<form>login stuff</form>" >> login.html
# Suddenly, a bug needs to be fixed on main IMMEDIATELY
# Can't commit the incomplete login form...
# Step 1: Stash the current work
git stash save "incomplete login form"
# Working directory is now clean
git status
# Output: nothing to commit, working tree clean
# Step 2: Switch to main and fix the bug
git switch main
echo "bug fixed" >> app.js
git add app.js
git commit -m "Fix critical bug in app.js"
# Step 3: Go back to the feature branch
git switch feature-login
# Step 4: Restore the stashed work
git stash pop
# The incomplete login.html is back — work can continue
git status
# Output: modified: login.html
Stash Including Untracked Files
By default, git stash only saves modified tracked files. To also stash brand new untracked files:
git stash -uOr the long form:
git stash --include-untrackedStash Everything Including Ignored Files
git stash -aShow the Contents of a Stash
# Show a summary of what is in the most recent stash
git stash show
# Show the full diff of the most recent stash
git stash show -p
# Show a specific stash
git stash show stash@{1} -p
Creating a Branch from a Stash
If the stashed changes no longer apply cleanly to the current branch (due to new commits), the stash can be applied to a brand new branch instead:
git stash branch new-branch-name stash@{0}This creates a new branch from the commit when the stash was made, applies the stash to it, and removes the stash from the list.
What Gets Stashed?
| File State | git stash | git stash -u | git stash -a |
|---|---|---|---|
| Modified tracked files | Yes | Yes | Yes |
| Staged files | Yes | Yes | Yes |
| New untracked files | No | Yes | Yes |
| Ignored files | No | No | Yes |
Summary
git stash is a lifesaver when switching context mid-work. It saves incomplete changes to a temporary stack so the working directory is clean. git stash pop retrieves and removes the most recent stash. Multiple stashes can be maintained with descriptive names for easy identification. Always use git stash save "message" to make stashes identifiable.
