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?

  • Trouble cloning a git repository from an EC2 instance
  • How to git tag all submodules?
  • How to keep file sync'd in OpenShift?
  • Annexed submodules in git
  • How can I skip a directory and its child directories when checkout out
  • Are merges in Git symmetric?
  • How to list modified files in multiple git in all sub directory in android source code?
  • Git - phantom local commits
  • git push php and host key verification failed
  • Git mode in terminal
  • Published using capistrano, is it possible to know which version is running using GIT? or anything?
  • Git Gui: How to exclude unnecessary files from rescan
  • 4 Solutions collect form web for “Why doesn't git format-patch work for stashes?”

    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

    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

    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.

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

    Git Baby is a git and github fan, let's start git clone.