Git Stash Tutorial: Save Work Without Committing
Git stash is one of the most useful commands for switching tasks without losing your work. This tutorial explains how to stash, list, apply, and drop stashes with practical examples.
Beginner8 min readUpdated
What is git stash?
Git stash temporarily shelves changes you've made to your working directory so you can switch context. The stashed changes are saved in a stack and can be reapplied later with git stash apply or git stash pop.
The short version
- 1Save changes:
git stash push -m 'description' - 2Switch to another task or branch
- 3List stashes:
git stash list - 4Restore work:
git stash pop - 5Drop old stashes:
git stash drop stash@{n} - 6Stash only specific files:
git stash push -- file1 file2
Why it matters
You're in the middle of a feature when a colleague pings: 'Can you quickly check out the bug on the login page?' You have half-finished changes everywhere. You're not ready to commit, and you can't switch branches with uncommitted changes.
Git stash lets you save your current work in progress without making a commit, switch to another task, and come back exactly where you left off. It's an essential daily-use command for any developer.
Where it usually goes wrong
- Losing stashes by accidentally dropping them
- Stashing and forgetting, then wondering where your work went
- Not naming stashes, making it impossible to tell them apart later
- Not knowing stash doesn't save untracked files by default
Git stash, step by step
Step 1: Stash your current changes
Run
git stashto save all tracked modified files and staged changes. Your working directory becomes clean.Terminal# Basic stash - saves tracked changesgit stash# Stash with a descriptive message (recommended)git stash push -m "WIP: user profile form validation"# Stash including untracked filesgit stash push --include-untracked -m "WIP: new auth module"# Stash including untracked AND ignored filesgit stash push --all -m "full workspace snapshot"Tip: Always add a message with
-m. After a few days, 'WIP on main: a3b2c1d message' means nothing.Step 2: List your stashes
Stashes are stored as a stack. The most recent stash is
stash@{0}. List all stashes to find what you saved.Terminalgit stash list# Output:# stash@{0}: On feature/auth: WIP: user profile form validation# stash@{1}: On main: WIP: dark mode toggle styles# stash@{2}: On main: hotfix experimentStep 3: Inspect a stash before applying
Before restoring a stash, check what changes it contains.
Terminal# View a summary of changed files in stash@{0}git stash show# View the full diff of stash@{0}git stash show -p# Inspect a specific stash by indexgit stash show -p stash@{1}Step 4: Apply a stash
Use
git stash popto apply and remove the stash from the stack, orgit stash applyto apply it and keep it in the stack.Terminal# Apply the most recent stash and remove it from the stackgit stash pop# Apply a specific stash and keep it in the stackgit stash apply stash@{1}# Apply the most recent stash to a different branchgit checkout other-branchgit stash popTip: Use
git stash popfor everyday use. Usegit stash applywhen you want to apply the same stash to multiple branches.In Gitoryx: All stashes are listed in a dedicated panel with their branch, timestamp, and message.How it works Step 5: Create a branch from a stash
If applying a stash causes conflicts (because the base branch changed significantly), create a new branch from the stash directly.
Terminal# Create a new branch from stash@{0} and apply it theregit stash branch feature/profile-form stash@{0}# This is equivalent to:# git checkout -b feature/profile-form <stash-base-commit># git stash apply stash@{0}# git stash drop stash@{0}Step 6: Stash only specific files or hunks (partial stash)
You don't have to stash everything. Use
--to stash specific files, or-p(patch mode) to interactively choose which hunks within a file to stash.Terminal# Stash a specific file onlygit stash push -m "WIP: profile form only" -- src/components/ProfileForm.tsx# Stash multiple specific filesgit stash push -m "WIP: auth changes" -- src/auth/login.ts src/auth/token.ts# Interactive patch mode - choose hunks to stash line by linegit stash push -p# Git will show each hunk and ask: stash this hunk? [y,n,q,a,d,/,e,?]# y = yes, n = no, q = quit (stash selected so far), a = all remainingTip: Patch mode (
-p) is the most precise way to stash: you can stash half of a file's changes and leave the other half in your working directory.Step 7: Clean up stashes
Remove individual stashes or clear the entire stack when you no longer need them.
Terminal# Drop a specific stashgit stash drop stash@{1}# Drop the most recent stashgit stash drop# Clear ALL stashes (irreversible)git stash clearCareful:
git stash clearis permanent. Stash entries are not recoverable once cleared (unless you know the object SHA fromgit fsck).
Mistakes to avoid with git stash
- Stashing untracked files without --include-untracked
- By default,
git stashonly saves tracked, modified files. New files you've created (untracked) are left behind, and switching branches leaves them visible. - Fix: Use
git stash push --include-untracked(or-u) to include new, untracked files in the stash. - Using unnamed stashes
- After a week with a dozen stashes,
stash@{3}: WIP on main: a1b2c3 fix button stylestells you almost nothing. - Fix: Always name your stashes:
git stash push -m 'WIP: descriptive name'. - Using git stash pop on the wrong branch
- Popping a stash on a branch that diverged from where the stash was created can cause unexpected conflicts.
- Fix: Check
git stash showfirst to see what branch and commit the stash was made on before applying.
Git stash in Gitoryx
Gitoryx shows all your stashes in a dedicated panel with their timestamps and messages. Apply, drop, or create a branch from a stash in one click.
- All stashes are listed in a dedicated panel with their branch, timestamp, and message.
- Create a stash with a name from the Gitoryx UI: no flags to remember.
- Apply, pop, or drop any stash with one click directly from the stash panel.
- Gitoryx shows a diff preview for each stash so you can inspect changes before applying.
- Create a branch from a stash in one step. Gitoryx handles the underlying commands automatically.
Free plan · no account required · macOS, Windows and Linux
Git stash: frequently asked questions
- What does git stash do?
- Git stash temporarily saves your current working directory and staging area changes without making a commit. It cleans your working directory so you can switch branches, then reapply your changes later.
- What is the difference between git stash pop and git stash apply?
git stash popapplies the stash and removes it from the stash stack.git stash applyapplies the stash but keeps it in the stack so you can apply it again later or to another branch.- Does git stash save untracked files?
- No, by default
git stashonly saves tracked (already added to git) modified files. To include untracked files, usegit stash push --include-untracked(or-u). To include all files (including ignored), use--all. - How do I stash only specific files?
- Use
git stash push -- path/to/fileto stash a specific file. For multiple files:git stash push -- file1 file2. For hunk-level control, usegit stash push -pto interactively choose which changes to stash. - Can I name a git stash?
- Yes, always name your stashes. Use
git stash push -m 'WIP: descriptive name'. Without a name, stashes show up asWIP on main: a1b2c3 commit messagewhich becomes meaningless after a few days. - Can I stash on one branch and apply on another?
- Yes. Stashes are not tied to a specific branch. Use
git stash applyon any branch. If the base code has changed significantly, you may encounter merge conflicts when applying. - How do I recover a dropped stash?
- Run
git fsck --no-reflog | awk '/dangling commit/ {print $3}'to find orphaned commits. Thengit stash apply <SHA>to restore it. This only works before Git garbage-collects the objects. - How many stashes can I have?
- There is no built-in limit. Git stashes are stored as commits in
refs/stashand can accumulate indefinitely. Keep your stash list clean by dropping stashes you no longer need. - What is git stash branch?
git stash branch <name>creates a new branch starting from the commit where the stash was created, then applies and drops the stash. Useful when applying a stash causes conflicts on the current branch.
Related reading
- How to Undo a Commit in Git (The Right Way)Tutorial · 10 min
- Git Branching Strategy: A Complete GuideTutorial · 14 min
- Git Cherry Pick Tutorial: Apply Specific CommitsTutorial · 9 min