I have a small patch saved away in my git stash. I've applied it to my working copy using
git stash apply
git stash apply --reverse
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
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...