3

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?

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • I think you mean `stash@{0}` instead, `git stash list` shows that as the name for me. Using just stash{0} returns an error about the revision not existing. I don't have an answer why it doesn't work though. – jonescb Jan 04 '11 at 17:38
  • @jonescb: right, that was just a typo, sorry for that. – UncleZeiv Jan 04 '11 at 18:16
  • I should probably mention that at the moment I have to use git `1.6.6`. – UncleZeiv Jan 04 '11 at 18:25
  • It also depends of the shell you are using, sometimes you have to escape `{` and `}` though. It happens to me when I’m using either zsh or fish. It becomes then `git format-patch -1 stash@\{1\} > mypatch.txt` – renoirb Dec 19 '14 at 23:07

4 Answers4

4

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
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • Your reasoning makes sense, but your proposed solution still gives an empty commit. If you have other solutions in mind, please note that it's ok for me to use the sha ids of the commits directly, instead of the `stash@{0}` notation. – UncleZeiv Jan 04 '11 at 18:24
1

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

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • thanks, I'm aware about this, but I was looking for a way to generate a patch compatible with `git am`, plus I was curious about what's happening anyway. – UncleZeiv Jan 04 '11 at 18:18
  • @unclezeiv Are you somehow tied to `git am` or can you use `git apply`? – Kelvin Aug 16 '11 at 21:06
0

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.

Community
  • 1
  • 1
Kelvin
  • 20,119
  • 3
  • 60
  • 68
-1

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

nakeer
  • 641
  • 1
  • 9
  • 17