← Back to blog

Git stash explained: save, apply, pop, and drop

tutorial git

What Is Git Stash?

You are halfway through a feature when something urgent comes up. A bug needs fixing on the main branch, or a teammate needs you to review their pull request. You are not ready to commit your work-in-progress, but you cannot switch branches with uncommitted changes cluttering your working directory. This is exactly the problem git stash was built to solve.

Git stash takes your uncommitted changes -- both staged and unstaged -- and saves them to a temporary storage area. Your working directory goes back to a clean state, matching the last commit. When you are ready, you can bring those changes back. Think of it as putting your work on a shelf so you can deal with something else, then picking it back up right where you left off.

Basic Usage: Stash, Apply, and Pop

Saving changes to the stash

The simplest form is just:

git stash

This takes all tracked modified and staged files and stores them. Your working directory becomes clean. Note that untracked files (files you have never added to Git) are not stashed by default. To include them, use:

git stash -u

Or to include everything, even ignored files:

git stash -a

Bringing changes back with apply

Once you have finished your side task and want your stashed work back, use:

git stash apply

This restores the stashed changes to your working directory but keeps the stash entry intact. The stash is still on the shelf, which can be useful if you want to apply the same changes to multiple branches.

Bringing changes back with pop

git stash pop

Pop works like apply, but it also removes the stash entry after applying it. This is the more common choice when you simply want your work back and do not need to keep the stash around.

The key difference: apply keeps the stash, pop deletes it after restoring. Use apply when you might need the stash again. Use pop when you are done with it.

Adding a Message to Your Stash

By default, Git labels stashes with an auto-generated description like WIP on main: a3b4c5d commit message. That is not very helpful when you have several stashes. You can add a descriptive message:

git stash push -m "refactoring the auth module"

This makes it much easier to identify what each stash contains when you list them later.

Managing Multiple Stashes

Listing your stashes

Git maintains a stack of stashes. You can see them all with:

git stash list

Output looks something like:

stash@{0}: On main: refactoring the auth module
stash@{1}: WIP on feature/checkout: 9f2e1d0 add cart logic
stash@{2}: WIP on main: 7c8d9e1 update dependencies

The most recent stash is always stash@{0}. Each new stash pushes older ones down the stack.

Applying or popping a specific stash

By default, apply and pop operate on the most recent stash. To target a specific one, reference it by its index:

git stash apply stash@{2}
git stash pop stash@{1}

Viewing what a stash contains

Before applying a stash, you might want to see what is inside it:

git stash show stash@{0}

This shows a summary of changed files. For the full diff, add -p:

git stash show -p stash@{0}

Stashing Specific Files

Sometimes you do not want to stash everything. Maybe you only want to set aside changes in one or two files. Since Git 2.13, you can do this with:

git stash push -m "sidebar layout changes" src/components/Sidebar.tsx src/styles/sidebar.css

Only the specified files are stashed. Everything else stays in your working directory.

Dropping and Clearing Stashes

Dropping a single stash

If you no longer need a specific stash, remove it with:

git stash drop stash@{1}

This deletes that stash entry. The remaining stashes are re-indexed automatically.

Clearing all stashes

To remove every stash in one go:

git stash clear

Be careful with this. There is no undo. All stashed changes are permanently deleted.

Creating a Branch from a Stash

Sometimes you stash changes, then realize they deserve their own branch. Instead of applying the stash and then creating a branch, Git offers a shortcut:

git stash branch new-feature-branch stash@{0}

This creates a new branch starting from the commit where the stash was originally made, applies the stash, and drops it. It is especially useful when applying the stash to your current branch would cause conflicts.

Common Pitfalls

  • Forgetting about stashes. Stashes are easy to forget. Run git stash list periodically to check if you have leftover stashes that should be applied or dropped.
  • Stash conflicts. If the code has changed significantly since you stashed, applying the stash can produce merge conflicts. Resolve them the same way you would resolve any merge conflict.
  • Relying on stash instead of commits. If you find yourself stashing frequently, consider committing your work-in-progress instead. You can always amend or squash those commits later. Stash is best for quick, short-lived context switches, not long-term storage.

Working with Stashes in GitSquid

GitSquid displays stashes directly on the commit graph, so you can see at a glance what stashes exist and which commit they were created from. Right-clicking a stash opens a context menu with options to apply, pop, drop, or clear all stashes. This makes managing stashes faster than typing commands, especially when you have multiple stashes and need to inspect or clean them up.

Quick Reference

Command What it does
git stash Stash tracked changes
git stash -u Stash including untracked files
git stash push -m "msg" Stash with a descriptive message
git stash list List all stashes
git stash apply Restore stash, keep the entry
git stash pop Restore stash, delete the entry
git stash drop stash@{n} Delete a specific stash
git stash clear Delete all stashes
git stash show -p View the full diff of a stash
git stash branch <name> Create a branch from a stash

Git stash is one of those tools that seems minor until you need it. Once you get comfortable with it, context-switching between tasks becomes seamless. Keep your stashes organized with messages, clean up old ones regularly, and use pop over apply when you do not need to keep the stash around.