git stash
reverse stash
git commands
version control
programming tips

How to reverse apply a stash?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Git does not have a dedicated command called "reverse apply stash," but you can undo a previously applied stash by applying the stash patch in reverse. The usual tool for that is git apply -R fed with the patch produced by git stash show -p.

This is useful when you applied a stash, decided you do not want those changes after all, and need to back them back out of the working tree. The key is to reverse the same diff that was originally applied.

The Core Command

To reverse the changes from a specific stash entry:

bash
git stash show -p stash@{0} | git apply -R

What this does:

  • 'git stash show -p stash@{0} prints the stash as a patch'
  • 'git apply -R applies that patch in reverse'

If stash@{0} is the stash you previously applied, this usually undoes those changes in the working tree.

A Safe Example Workflow

Suppose you did this:

bash
git stash push -m "wip"
git stash apply stash@{0}

Then you decide the apply was a mistake. Reverse it with:

bash
git stash show -p stash@{0} | git apply -R

This puts the working tree back closer to the state before the stash apply.

Check the Result Immediately

After reversing, inspect the working tree before doing anything else:

bash
git status
git diff

This matters because the reverse apply can fail partially if the current files no longer match the exact state expected by the patch.

A stash apply is just a patch-like change set. If the file contents have drifted since then, the reverse may produce conflicts or reject some hunks.

When the Stash Was Popped

If you used git stash pop, the stash entry may already be gone even though the changes were applied. In that case, reversing from stash@{0} may not be possible because the stash object no longer exists.

If you realize the mistake immediately, check whether the stash commit is still discoverable in the reflog or stash reflog. But if the stash still exists, using the explicit stash reference is much easier and safer.

That is one reason git stash apply is sometimes safer than pop when you are not sure you want to keep the result yet.

Reverse Apply Versus Resetting Everything

Sometimes developers use git reset --hard to undo an applied stash. That can work if you truly want to discard all uncommitted changes, but it is much broader and more destructive.

By contrast, reversing the stash patch targets the stash changes specifically:

bash
git stash show -p stash@{0} | git apply -R

This is often the better tool when the stash changes are the thing you want to undo, not every uncommitted change in the working tree.

If You Want a Safer Dry Run Mindset

Before reversing in a messy working tree, consider isolating the changes first. For example, commit or stash unrelated work so the reverse operation has a cleaner surface to act on.

The reverse apply is most reliable when the working tree still closely resembles the state that existed right after the stash was applied.

Common Pitfalls

The biggest pitfall is assuming reverse apply is guaranteed to work cleanly no matter what happened after the stash was applied. If files changed in the meantime, patch context can drift and create conflicts.

Another common mistake is using git stash pop and then expecting the stash reference to still be available. pop removes the stash entry after a successful apply, which makes later reversal harder.

People also sometimes run git reset --hard out of frustration. That may remove more changes than intended if other uncommitted work is present.

Finally, always inspect git status and git diff after reversing. Do not assume the working tree is exactly where you want it without checking.

Summary

  • Git does not have a built-in command named "reverse apply stash," but you can reverse the stash patch.
  • The usual command is git stash show -p stash@{n} | git apply -R.
  • This works best when the stash entry still exists and the working tree has not drifted too far.
  • 'git stash apply is often safer than git stash pop if you may want to undo the apply later.'
  • Check git status and git diff immediately after the reverse apply.

Course illustration
Course illustration

All Rights Reserved.