How to reverse apply a stash?

git apply --reverse
git stash apply
how to undo a failed git stash pop
git apply reverse
stash log
how to revert git stash changes
git stash -k
git stash interactive

I have a small patch saved away in my git stash. I've applied it to my working copy using git stash apply. Now, I'd like to back out those changes by reverse applying the patch (kind of like what git revert would do but against the stash).

Does anyone know how to do this?

Clarification: There are other changes in my working copy. My particular case is hard to describe but you can imagine some debugging or experimental code that's in the stash. Now it's mixed in my working copy with some other changes and I'd like to see the effect with and without the changes from the stash.

It doesn't look like stash supports this currently, but a git stash apply --reverse would be a nice feature.


According to the git-stash manpage, "A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created," and git stash show -p gives us "the changes recorded in the stash as a diff between the stashed state and its original parent.

To keep your other changes intact, use git stash show -p | patch --reverse as in the following:

$ git init
Initialized empty Git repository in /tmp/repo/.git/

$ echo Hello, world >messages

$ git add messages

$ git commit -am 'Initial commit'
[master (root-commit)]: created 1ff2478: "Initial commit"
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 messages

$ echo Hello again >>messages

$ git stash

$ git status
# On branch master
nothing to commit (working directory clean)

$ git stash apply
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   messages
#
no changes added to commit (use "git add" and/or "git commit -a")

$ echo Howdy all >>messages

$ git diff
diff --git a/messages b/messages
index a5c1966..eade523 100644
--- a/messages
+++ b/messages
@@ -1 +1,3 @@
 Hello, world
+Hello again
+Howdy all

$ git stash show -p | patch --reverse
patching file messages
Hunk #1 succeeded at 1 with fuzz 1.

$ git diff
diff --git a/messages b/messages
index a5c1966..364fc91 100644
--- a/messages
+++ b/messages
@@ -1 +1,2 @@
 Hello, world
+Howdy all

Edit:

A light improvement to this is to use git apply in place of patch:

git stash show -p | git apply --reverse

Alternatively, you can also use git apply -R as a shorthand to git apply --reverse.

I've been finding this really handy lately...

Git: Reverse a stash apply (Example), Sometimes I want to undo a git stash apply. git stash show -p | git apply --reverse. That combines: Show the changes recorded in the stash as a  If you do a reset --hard without stashing first, you can still see your patch in .git/refs/stash, as described in "Undo git reset --hard after git stash pop" (by a git stash apply would not remove the patch from the stash anyway, like git stash pop does, so here you don't have to worry about that), or you can recover it from git fsck.


git stash[save] takes your working directory state, and your index state, and stashes them away, setting index and working area to HEAD version.

git stash apply brings back those changes, so git reset --hard would remove them again.

git stash pop brings back those changes and removes top stashed change, so git stash [save] would return to previous (pre-pop) state in this case.

Undo git stash apply, Sometimes we may need to undo a git stash apply, maybe we didn't mean to apply it at all or we just applied it to the wrong branch. Show the changes recorded in the stash as a diff between the stashed state and its original parent. When no <stash> is given, shows the latest one. the diffstat, but it will accept any format known to git diff (e.g., git stash show -p stash@{1} to view the second most recent stash in patch form). Apply the patch in reverse.


git checkout -f

will remove any non-commit changes.

Git stash drop vs git reset, brings back those changes, so git reset --hard would remove them again. Building on what you were trying to do before, you could store the patch from the stash and remove the entries for files that were conflicted. Then you would resolve the conflicts with checkout --ours, and reverse-apply the rest of the patch. Of you could handle it file by file, using . git stash show --name-only


The V1 git man page had a reference about un-applying a stash. The excerpt is below.

The newer V2 git man page doesn't include any reference to un-applying a stash but the below still works well

Un-applying a Stash In some use case scenarios you might want to apply stashed changes, do some work, but then un-apply those changes that originally came from the stash. Git does not provide such a stash un-apply command, but it is possible to achieve the effect by simply retrieving the patch associated with a stash and applying it in reverse:

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

Again, if you don’t specify a stash, Git assumes the most recent stash:

$ git stash show -p | git apply -R

You may want to create an alias and effectively add a stash-unapply command to your Git. For example:

$ git config --global alias.stash-unapply '!git stash show -p | git apply -R'
$ git stash apply
$ #... work work work
$ git stash-unapply

Git stash twice, you've made to your working copy so you can work on something else, and then come back and re-apply them later on. If you originally ran git stash apply, then that stash is still present in the repo. You can do whatever you need to do to get this branch back to a good state — revert, reset, rebase, resolve, whatever. Then later on, you can check out the correct branch and apply the original stash.


This is long over due, but if i interpret the problem correctly i have found a simple solution, note, this is an explanation in my own terminology:

git stash [save] will save away current changes and set your current branch to the "clean state"

git stash list gives something like: stash@{0}: On develop: saved testing-stuff

git apply stash@{0} will set current branch as before stash [save]

git checkout . Will set current branch as after stash [save]

The code that is saved in the stash is not lost, it can be found by git apply stash@{0} again.

Anywhay, this worked for me!

git stash, git checkout new-branch; git rebase master. In order to apply the correct stashed changes (now 2nd on the stack):. $ git stash apply stash@{1}  git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to


Undo git stash pop that results in merge conflict, git stash apply On branch master Changes not staged for commit: (use "git add stash@{1}: WIP on master: c264051 Revert "Add file_size" stash@{2}: WIP on  You basically have to apply the stash, then only stage the parts that you want to commit. You can then re-stash the remainder (or discard it). You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.


Stashing and Cleaning, Save your local modifications to a new stash, and run git reset --hard to revert them. Remove a single stashed state from the stash list and apply it on top of the  and it will unstash your changes. If you want to preserve the state of files (staged vs. working), use. git stash apply --index. Share a link to this answer. improve this answer. edited May 2 '16 at 18:44. 17 silver badges. 34 bronze badges. answered May 31 '12 at 3:46. 29 silver badges. 26 bronze badges. Well, not quite. It will not reset what


git-stash Documentation, Re-applying your stashed changes. You can reapply previously stashed changes with git stash pop : $ git status On branch master nothing to commit, working  The Stashes are visible on the "left sidebar," situated between the Bookmarks and the File Status, Log / History, and Search views. There is a context menu for each Stash that will let you Apply or Delete a saved stash.