git stash lets you put unfinished work aside, switch tasks and come back later without making messy commits. In this lesson you will save, inspect and restore stashes, handle untracked files and conflicts, and learn when a WIP commit is better.
๐ What You’ll Learn: what git stash is, basic stash and pop, stashing with a message, listing and inspecting stashes, pop vs apply, drop and clear, untracked and partial stashes, git stash branch, conflicts when popping, and common real-world situations.
๐ฆ What Is git stash?
You are halfway through a change when an urgent bug report arrives, or you need to pull, or switch branches, but your work is not ready to commit. git stash saves your uncommitted changes on a stack, cleans your working directory, and lets you bring the changes back later.
Think of it as a clipboard for unfinished work.
โถ๏ธ Basic Usage
git status # modified: tests/login.spec.ts
git stash # save the changes and clean the working directory
git status # nothing to commit, working tree clean
# ...switch branch, pull, fix the urgent bug...
git stash pop # bring the changes back and remove them from the stash
Saved working directory and index state WIP on main: 8b1d4e2 Add learning goals
๐ท๏ธ Stash with a Message
With several stashes, messages tell them apart:
git stash push -m "WIP: login tests for locked-out user"
๐ List, Inspect and Choose Stashes
git stash list
stash@{0}: On feature/login: WIP: login tests for locked-out user
stash@{1}: WIP on main: 8b1d4e2 Add learning goals
stash@{0} is always the most recent.
git stash show stash@{1} # files changed in a stash
git stash show -p stash@{1} # the full diff
๐ pop vs apply
| Command | Restores changes | Removes the stash from the list |
|---|---|---|
git stash pop | โ | โ (only if it applied without conflicts) |
git stash apply | โ | โ Keeps it, so you can apply it again elsewhere |
git stash apply stash@{1} # apply a specific stash and keep it
git stash drop stash@{1} # delete one stash
git stash clear # delete ALL stashes (cannot be undone)
โ ๏ธ Common mistake: git stash clear and git stash drop permanently delete stashed work. List with git stash list before cleaning up.
๐งฉ Untracked and Ignored Files
By default, git stash saves only tracked files (modified and staged). New files stay in the working directory.
| Command | Also stashes |
|---|---|
git stash -u (--include-untracked) | New, untracked files |
git stash -a (--all) | Untracked and ignored files |
git stash push -- tests/login.spec.ts | Only the listed file(s) |
git stash push --staged | Only staged changes (Git 2.35 and newer) |
git stash --keep-index | Stash, but leave staged changes in place |
๐ฟ Turn a Stash into a Branch
If the code changed a lot since you stashed and pop would conflict, create a branch from the commit where you stashed and apply it there:
git stash branch feature/login-wip stash@{0}
๐งฏ Conflicts When Popping
If the stashed changes clash with changes made since, git stash pop reports conflicts like a merge. Resolve them as in Resolve Merge Conflicts, then git add the files. The stash is not dropped automatically when there were conflicts; drop it yourself once you are happy: git stash drop.
๐งช Typical Situations
| Situation | Commands |
|---|---|
| Pull when you have local edits | git stash, git pull, git stash pop |
| Urgent fix on another branch | git stash -u, git switch main, fix and commit, git switch -, git stash pop |
| Started work on the wrong branch | git stash, git switch correct-branch, git stash pop |
| Try an idea quickly and discard it | git stash to save current work, experiment, git restore ., git stash pop |
๐ก Tip: Stashes are local and easy to forget. For anything longer than a quick interruption, a WIP commit on a feature branch is often safer: you can tidy it later with interactive rebase.
๐ Assignment
- Modify two tracked files, stash them, confirm a clean
git status, and restore them withpop. - Create three stashes with messages and list them.
- Inspect the second stash with
git stash show -p, apply it withapply, and drop it. - Create a new untracked file and show that
git stashignores it butgit stash -usaves it. - Simulate “started on the wrong branch”: edit on
main, stash, switch to a new branch, pop. - Bonus: create a conflict when popping a stash and resolve it.
โ FAQ
๐ค What does git stash do?
It saves your uncommitted changes in a temporary stack and restores a clean working directory, so you can switch tasks and reapply the changes later.
๐ค What is the difference between git stash pop and git stash apply?
Both restore the changes. pop also removes the stash from the list; apply keeps it.
๐ค How do I stash untracked files?
Use git stash -u (or --include-untracked).
๐ค How do I name a stash?
git stash push -m "your message".
๐ค Can I recover a dropped stash?
Sometimes. Dropped stashes become unreachable commits; git fsck --unreachable can list them while they still exist. It is much easier not to drop stashes you might need.
๐ฏ Next Steps
- ๐ git cherry-pick: Copy Commits Between Branches
- ๐ท๏ธ Git Tags and Versioning
- ๐ Git and GitHub Tutorial: full course
๐ฌ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in
3 thoughts on “git stash: Save, List, Apply and Pop Work in Progress”