Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format patch with what I stash away

People also ask

How do I apply a patch format?

If you're using a JetBrains IDE (like IntelliJ IDEA, Android Studio, PyCharm), you can drag the patch file and drop it inside the IDE, and a dialog will appear, showing the patch's content. All you have to do now is to click "Apply patch", and a commit will be created.

How do you get your changes back after stash?

To retrieve changes out of the stash and apply them to the current branch you're on, you have two options: git stash apply STASH-NAME applies the changes and leaves a copy in the stash. git stash pop STASH-NAME applies the changes and removes the files from the stash.

What happens when I stash my changes?

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.


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

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

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.


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.


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.