DEV Community

Cover image for Git Stash — The Complete Guide Every Developer Should Know
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on

Git Stash — The Complete Guide Every Developer Should Know

When you're working on a project, it’s very common to start coding something and suddenly realize you need to switch branches, fix a bug, or pull new changes. But your current work is incomplete and not ready to commit.

This is exactly where Git Stash becomes one of the most powerful tools in your workflow.

In this article, we will explore Git Stash in depth — from basics to advanced usage — with real-world scenarios and clear explanations.


🚀 What is Git Stash?

Git Stash temporarily saves your uncommitted changes (both staged and unstaged) so you can work on something else and come back later.

Think of it as a temporary storage (clipboard) for your code changes.


🧠 Why Use Git Stash?

Here are common scenarios:

  • You are in the middle of work but need to switch branches
  • You need to quickly fix a bug in production
  • You want to pull latest changes but your working directory is dirty
  • You want a clean working tree without committing incomplete work

📦 Basic Usage

1. Save Changes

git stash
Enter fullscreen mode Exit fullscreen mode

This command:

  • Saves your modified tracked files
  • Reverts your working directory to the last commit

2. View Stashes

git stash list
Enter fullscreen mode Exit fullscreen mode

Example output:

stash@{0}: WIP on main: 123abc Fix login bug
stash@{1}: WIP on feature: 456def Add new API
Enter fullscreen mode Exit fullscreen mode

3. Apply Stash

git stash apply
Enter fullscreen mode Exit fullscreen mode

This restores the latest stash but keeps it in the stash list.


4. Apply and Remove (Pop)

git stash pop
Enter fullscreen mode Exit fullscreen mode

This:

  • Applies the stash
  • Removes it from the stash list

5. Drop a Stash

git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

Deletes a specific stash.


6. Clear All Stashes

git stash clear
Enter fullscreen mode Exit fullscreen mode

⚠️ Warning: This deletes all stashes permanently.


🔍 Stashing Specific Changes

Stash Only Staged Changes

git stash --staged
Enter fullscreen mode Exit fullscreen mode

Stash Including Untracked Files

git stash -u
Enter fullscreen mode Exit fullscreen mode

or

git stash --include-untracked
Enter fullscreen mode Exit fullscreen mode

Stash Everything (Including Ignored Files)

git stash -a
Enter fullscreen mode Exit fullscreen mode

🏷️ Naming Your Stash

Instead of default messages, you can name your stash:

git stash push -m "WIP: payment integration"
Enter fullscreen mode Exit fullscreen mode

This helps a lot when managing multiple stashes.


🔄 Applying Specific Stash

git stash apply stash@{1}
Enter fullscreen mode Exit fullscreen mode

🔀 Create Branch from Stash

One of the most useful features:

git stash branch new-branch-name
Enter fullscreen mode Exit fullscreen mode

This:

  • Creates a new branch
  • Applies the stash
  • Removes it from stash list

Perfect when your temporary work becomes important.


⚠️ Handling Conflicts

When applying a stash, conflicts may occur if:

  • The same lines were changed
  • The codebase has evolved

Git will mark conflicts just like a merge:

<<<<<<< HEAD
Your current code
=======
Stashed code
>>>>>>> stash
Enter fullscreen mode Exit fullscreen mode

You must resolve these manually.


🧩 Internal Working (Advanced)

When you run:

git stash
Enter fullscreen mode Exit fullscreen mode

Git actually creates:

  • A commit for your working directory
  • A commit for your index (staged changes)

These are stored in a special reference:

refs/stash
Enter fullscreen mode Exit fullscreen mode

So stash is not magic — it's structured commits behind the scenes.


🔥 Real-World Workflow Example

Scenario:

You're working on a feature:

git checkout feature-auth
Enter fullscreen mode Exit fullscreen mode

You modify files but suddenly:

👉 You must fix a bug in main

Solution:

git stash
git checkout main
# fix bug
git commit -m "hotfix: login issue"
git checkout feature-auth
git stash pop
Enter fullscreen mode Exit fullscreen mode

You’re back exactly where you left off.


💡 Best Practices

  • Use descriptive stash messages
  • Avoid keeping too many stashes
  • Prefer branches for long-term work
  • Use git stash pop carefully (it deletes stash)

❌ Common Mistakes

  1. Forgetting stash exists → losing track of work
  2. Using stash instead of proper commits
  3. Not naming stashes → confusion later
  4. Clearing stash accidentally

🧠 Pro Tips

  • Use git stash show -p to see full diff:
git stash show -p stash@{0}
Enter fullscreen mode Exit fullscreen mode
  • Stash only part of changes (interactive):
git stash -p
Enter fullscreen mode Exit fullscreen mode
  • Combine with rebase or pull for cleaner workflow

🎯 Conclusion

Git Stash is a powerful, flexible tool that helps you:

  • Switch context quickly
  • Keep your work safe
  • Maintain a clean workflow

But remember:

👉 It’s a temporary solution, not a replacement for commits or branches.

Mastering Git Stash will significantly improve your productivity and confidence when working with Git.

Top comments (0)