Git Stash
Park work in progress safely. Apply vs pop, untracked files, naming and inspecting stashes, and when a branch is the better tool.
What Git Stash Is For
Git stash lets you temporarily set aside local changes without committing them. It is useful when you are halfway through one task and suddenly need to switch branches to fix something urgent.
Instead of creating a messy “work in progress” commit, you can stash the changes, do the urgent task, and then restore your original work later.
Tip: Stash is great for short-term interruptions. For longer work, a real branch and commit are usually safer.
Creating a Basic Stash
Suppose you modified app.js and README.md but are not ready to commit.
Check the status:
git status
Now stash the tracked changes:
git stash
Git saves the current state and reverts your working directory to match the last commit.
You can confirm with:
git status
The working tree should now be clean.
Listing Stashes
To see saved stashes:
git stash list
Example output:
stash@{0}: WIP on feature/login: a1b2c3d Add login page
stash@{1}: WIP on main: d4e5f6g Update README
Each stash has an identifier like stash@{0}.
Restoring a Stash with apply
Use git stash apply when you want to restore a stash but keep it in the stash list.
git stash apply stash@{0}
This is useful if you want to test the restored changes before deciding whether to remove the stash entry.
If you omit the name, Git applies the latest stash:
git stash apply
Restoring and Removing with pop
Use git stash pop when you want to restore a stash and remove it from the stash list in one step.
git stash pop
This is the most common restore command.
apply vs pop
apply: restore but keep the stash savedpop: restore and delete the stash entry if successful
Naming a Stash
Default stash messages are not always easy to remember. You can create a named stash:
git stash push -m "half-finished navbar styling"
List it later:
git stash list
Named stashes are much easier to identify when you have several saved states.
Stashing Untracked Files
By default, git stash saves changes to tracked files only. If you also want to include untracked files, use -u.
git stash push -u -m "save new config files too"
This is helpful when you created new files but are not ready to commit them yet.
Note: Untracked files are files Git sees but is not yet tracking. Ignored files are a separate category and are not included unless you use additional options.
Dropping and Clearing Stashes
To delete one stash entry:
git stash drop stash@{0}
To remove all stash entries:
git stash clear
Be careful with clear because it removes everything from the stash list.
A Real Workflow Example
Imagine you are on feature/docs editing tutorial content when a production issue appears.
git status
git stash push -m "draft updates for Git basics"
git switch main
git switch -c hotfix/header-bug
# fix the issue
git add .
git commit -m "Fix broken header in production"
git switch feature/docs
git stash pop
This lets you pause one task, fix another, and return without creating a confusing temporary commit.
Handling Stash Conflicts
Applying a stash can sometimes create conflicts, especially if the branch changed since the stash was created.
If that happens:
- run
git status - resolve the conflicts manually
- stage the resolved files with
git add - continue working or commit the result
A stash is not magic. It still depends on how different the files became over time.
When Not to Use Stash
Stash is not ideal for everything.
Avoid relying on stash when:
- you need a durable backup of important work
- the work should be reviewed by others
- you are pausing for a long period
- you are working across multiple machines
In those cases, a normal branch and commit are usually better.
What You Should Remember
Git stash is a temporary shelf for unfinished work. Use stash to save changes quickly, list to inspect saved entries, apply or pop to restore them, and drop or clear to clean them up. Use -u for untracked files and -m for a helpful message.
For quick interruptions, stash is excellent. For meaningful milestones, commit to a branch instead.
Test Your Understanding
Exercise 1: Pausing work quickly
Scenario: You are halfway through editing app.js and README.md, and an urgent bug forces you to switch tasks immediately.
Question: Which Git feature from this lesson is designed for that short-term interruption without making a work-in-progress commit?
Exercise 1: Pausing work quickly
Choose the Git feature intended for temporarily shelving unfinished local changes.
Exercise 2: Saving untracked files too
Scenario: Your unfinished work includes brand-new config files that are still untracked, and you want a descriptive stash entry.
Question: Which command best matches the tutorial?
Exercise 2: Saving untracked files too
Choose the stash command that includes untracked files and adds a helpful message.
Exercise 3: Keeping the stash after restore
Scenario: You want to restore a stash to test something, but you do not want the stash entry removed yet.
Question: Which command should you prefer?
Exercise 3: Keeping the stash after restore
Pick the restore command that leaves the stash entry in the list.
Frequently Asked Questions
What is the difference between git stash apply and git stash pop?
Both restore the stashed changes to your working directory. pop then deletes the stash entry; apply leaves it in the list. Use apply when you want to put the same work onto more than one branch, or when you are not yet sure the restore will go cleanly, if it conflicts badly you still have the stash to fall back on. Use pop for the ordinary case of picking work back up once. Note that pop does not delete the entry if the restore hits a conflict, which is deliberate.
Does git stash include untracked files?
No, and this surprises people regularly. A plain git stash leaves new files sitting in your working directory, so switching branches carries them along and a "clean" stash turns out not to be. Use git stash -u (or --include-untracked) to include them, and git stash -a (--all) to include ignored files as well. Because -a sweeps up build output and node_modules, it is rarely what you want.
How do I see what is in a stash before restoring it?
git stash list shows the entries with their references. git stash show stash@{0} gives a summary of changed files, and git stash show -p stash@{0} prints the full diff. You can also restore a single file from a stash without applying the whole thing: git checkout stash@{0} -- path/to/file. Naming stashes as you create them, git stash push -m "half-done search filter", makes the list readable a week later, which the default WIP on main messages do not.
I dropped a stash by accident. Can I recover it?
Usually yes. A dropped stash is an unreachable commit, and git fsck --unreachable | grep commit lists candidates you can inspect with git show <sha>. Once you find it, git stash apply <sha> restores the contents. It works until Git garbage-collects, which is typically not for weeks. The wider lesson is that stashes are the least durable place to keep work. A throwaway commit on a scratch branch is recoverable through the reflog and cannot be dropped by a stray git stash clear.
How do I apply a stash to a different branch?
Just switch branches and apply it. Stashes are not tied to the branch they were created on. git switch other-branch then git stash pop is a standard way to move work you started in the wrong place. Conflicts are possible if the branches have diverged in the same files, and git checkout --theirs/--ours plus normal conflict resolution applies. If you expect trouble, use apply rather than pop so the stash survives a failed attempt.
When should I use a branch instead of a stash?
Whenever the work will outlive the next half hour. A stash is an unnamed, unordered pile that is easy to forget and easy to clear, and git stash list after a busy fortnight is nobody's friend. For anything you intend to return to, commit it on a branch, even as a scrappy "wip" commit you later amend or squash. Stash is at its best for exactly what it was designed for: parking a few minutes of work to pull, switch branch for a quick fix, or check something on a clean tree.