Git Stash: A Practical Guide

You are halfway through a feature, someone pings you about a production bug, and you need to switch branches immediately. Your working directory is a mess of uncommitted changes. This is exactly what git stash solves — it shelves your changes temporarily so you can work on something else without committing incomplete work.

What is Git Stash?

Git stash takes your modified tracked files and your staged changes, saves them on a stack of unfinished changes, and reverts your working directory to match the HEAD commit. You can then restore those changes later.

Basic Stash Operations

Save Changes

git stash

This saves your uncommitted changes and cleans your working directory. You can add a message to remember what the stash contains:

git stash push -m "WIP: login form validation"

List Your Stashes

git stash list

Output:

stash@{0}: On main: WIP: login form validation
stash@{1}: On feature-x: partial API refactor

Restore the Most Recent Stash

git stash pop

Applies the most recent stash and removes it from the stash list. If there are merge conflicts, the stash is not removed — you resolve the conflicts and then run git stash drop manually.

Restore Without Removing

git stash apply stash@{0}

Applies the stash but keeps it in the list. Useful when you want to apply the same changes to multiple branches.

Remove Stashes

# Drop a specific stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Practical Scenarios

Switch Branches Mid-Feature

git stash push -m "half-done sidebar work"
git checkout main
git pull
# fix the bug
git checkout -
git stash pop

Stash Only Specific Files

git stash push -m "just the config" -- config.yml .env

This stashes only the listed files, leaving everything else in your working directory untouched.

Include Untracked Files

By default, git stash does not save untracked files (new files not yet added to Git). To include them:

git stash push --include-untracked

View Stash Diff Before Applying

git stash show -p stash@{0}

This shows the full diff of what the stash contains, so you can verify the changes before applying them.

Common Mistakes

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles