Git Stash: Save and Restore Changes
Learn how to use git stash to temporarily save uncommitted changes and restore them later. Switch branches without losing your work.
git stash push -m "work in progress"Detailed Explanation
What Does git stash Do?
git stash takes your uncommitted changes — both staged and unstaged — and saves them on a stack so you can work on something else. Your working directory is then reverted to the last commit, giving you a clean slate.
Basic Usage
# Stash current changes with a descriptive message
git stash push -m "work in progress on login form"
# List all stashes
git stash list
# Restore the most recent stash (and remove it from the stack)
git stash pop
# Restore without removing from the stack
git stash apply
Stashing Untracked Files
By default, git stash only saves tracked files. To include untracked files:
git stash push -u -m "include new files"
To also include ignored files, use -a (all).
Working with Multiple Stashes
Each stash is indexed as stash@{0}, stash@{1}, etc. You can apply or drop a specific stash:
# Apply a specific stash
git stash apply stash@{2}
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes
git stash clear
Creating a Branch from a Stash
If your stashed changes conflict with your current branch, create a new branch from the stash:
git stash branch new-feature-branch stash@{0}
This checks out the commit where the stash was created, applies the stash, and drops it — all in one step.
Tips
Always use the -m flag to label your stashes. Without messages, the stash list becomes difficult to navigate. Think of stash as a short-term clipboard, not long-term storage — commit or branch off when the work is meaningful.
Use Case
A developer is halfway through a feature when an urgent bug report comes in. They stash their current work, switch to the hotfix branch, and restore the stash afterward.