183

In git, I stash away my changes. Is it possible that I can create a patch with what I stash away? And then apply that patch in some other repository (my co-worker's)?

I know git format-patch -1, but I think that it's for what I have committed. But I am looking for the same thing for changes that I stashed away.

And how can I apply a patch in other repository?

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
silverburgh
  • 8,659
  • 10
  • 31
  • 24

5 Answers5

229

Sure, git stash show supports this:

git stash show -p

So, use

git stash list

to find out the number of the stash that you want to export as a patch, then

git stash show -p stash@{<number>} > <name>.patch

to export it.

For example:

git stash show -p stash@{3} > third_stash.patch
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    I have a related question about apply a patch. Let's say my patch touches multiple files. Is there a way to apply the patch 'interactively'? Pick which files of the patch I should apply the patch against? Can I do that? – silverburgh Jan 30 '10 at 01:43
  • 1
    @silverburgh: I had a quick look through `man patch` and I didn't see any options for interactive patch application. However, since patch files are plain text files themselves, usually what one would do is edit the patch in a text editor to clip out the relevant parts to apply with `patch`. Alternately, if you're applying the patch into another Git repository, you could apply it all and then selectively `git checkout` files that you didn't want to change (`git checkout` with a filename throws away unstaged changes). – Greg Hewgill Jan 30 '10 at 02:08
  • 1
    @silverburgh you can restrict the set of files patched using the "--exclude" and "--include" params of git apply. – Kelvin Aug 16 '11 at 19:24
  • @silverburgh you can do the following assume you have a patch. apply the patch fully and then do `git add --interactive ${YOUR_FILES}` and it will give you the chance to partially commit. – Alexander Oh Feb 08 '13 at 13:18
  • I needed to use this, to shut off the color: git stash show -p --color=never > wip.patch – Paul Beckingham Apr 01 '13 at 20:27
  • 16
    Thanks. This worked for me: `git stash show -p stash@{1} > patch.txt` – Ryan May 30 '14 at 15:51
  • This didn't seem to work if your stash contained new files! See https://stackoverflow.com/questions/22818155/git-stash-to-patch-with-untracked-files – Ryan Jan 17 '19 at 00:47
  • stash filter should be before the p option, at least on windows git: `git stash show stash@{} -p > .patch` – Greg Mar 04 '21 at 13:42
  • The created patch fails to work with `git am -- .patch`. Outputs "Patch format detection failed." – tim-montague Jun 13 '22 at 00:11
82

This answer provides info about both saving the patch and applying it where you want to use it.

To stash the output in a file:

 git stash show -p --color=never > my-patch-name.patch

Verify patch looks good:

git apply --stat my-patch-name.patch

Verify no errors:

git apply --check my-patch-name.patch

Apply the patch

git apply my-patch-name.patch
calvinf
  • 3,754
  • 3
  • 28
  • 41
  • 1
    This one worked for me with plain text code files, but I had to account for white space. Verify patch looks good: `git apply --check --ignore-space-change --ignore-whitespace my-patch-name.patch` Apply the patch: `git apply --ignore-space-change --ignore-whitespace my-patch-name.patch` – Craig Boland Sep 16 '15 at 20:41
  • Nice and concise explanation. For it to work I had to be in the repo root when applying the patch, else `git apply` didn't pick up the diff. – Max Feb 28 '19 at 15:55
17

Use

$> git stash list
stash@{0}: WIP on master: 84fx31c Merged with change to /public/
stash@{1}: WIP on master: 463yf85 FlupResource: also takes json as a query parameter

to get a list of your recently stashed stuff. Git actually creates commit objects when you stash.

They are commits like everything else. You can check them out in a branch:

$> git checkout -b with_stash stash@{0}

You can then publish this branch and you colleague can merge or cherry-pick that commit.

peritus
  • 3,732
  • 2
  • 22
  • 17
17

Above solutions won't work for binary data. The following add support for it:

git stash show stash@{0} -p --binary

Edit

Note: I just wanted to add a comment to above replies but my reputation is not sufficient.

Davide Guerri
  • 1,887
  • 2
  • 17
  • 25
4

I believe this might be one of the udpates from Git recently. You don't have to patch the changes you stashed away any more. you can just apply your stashed changes on one branch to another.

Say on branch A you have stashed away some changes, referred as stash@{1}.

you now switch to branch B. you can just do:

$git stash apply stash@{1}

this applies your branch A changes onto branch B.

stucash
  • 1,078
  • 1
  • 12
  • 23