UncleZeiv
UncleZeiv

Reputation: 18488

Why doesn't git format-patch work for stashes?

If I run

git format-patch -1 stash@{0}

git returns silently without creating any file. Why does this happen? How can I save a stash in a format compatible with git am?

Upvotes: 3

Views: 1340

Answers (4)

nakeer
nakeer

Reputation: 767

If your files are stashed using git stash -u then git stash show -p doesn't work

Upvotes: -1

Kelvin
Kelvin

Reputation: 20857

I can think of 2 ways.

Solution 1: Create a branch from the stash. This kind of defeats the original purpose of the stash feature, which was to avoid having to create the separate branch.

Solution 2: Add all changes to tracked files to the index before stashing. After stashing, run

git format-patch 'stash@{0}^1'..'stash@{0}^2'

which compares HEAD with the index (at the time the stash was created).

I'm not sure why you can't just leave files unadded to the index and run git format-patch 'stash@{0}^1'..'stash@{0}'

which seems like the same thing. There must be something special about the stash commit object. In any case, adding to the index will record the changes in the stash's 2nd parent (the index commit), bypassing the issues with the stash commit.

Misc notes: git format-patch -1 will always be blank for a merge commit (stashes are a special case of a merge commit). I'm not entirely sure why this is, but see Git: How to create patches for a merge? for more detail.

Upvotes: 0

Abizern
Abizern

Reputation: 150565

You could try

git stash show -p > ~/Desktop/stash.patch

This will generate a patch file on your desktop for the latest patch and the original parent.

Described in the documentation of git-stash under the show option

Upvotes: 1

Josh Lee
Josh Lee

Reputation: 177520

This seems to be because the stash commit is represented as a merge (between its parent and the index state at the time), and format-patch on a merge commit does nothing.

If you say

git format-patch stash@{0}{,^}

then it will spit out patches between the stash and each parent.

For illustration, this is what the stash looks like:

*   99aedb8 (refs/stash) WIP on master: 668ff36 initial commit
|\  
| * 6b8d77f index on master: 668ff36 initial commit
|/  
* 668ff36 (HEAD, master) initial commit

Upvotes: 4

Related Questions