I would like to create a new GIT stash from a commit on a branch. Is this even possible?
-
1You mean the thing you get with `git stash save`, right? What do you intend to do with the commit itself? – torek May 05 '16 at 22:34
-
Because of the way our branch management works I need to apply the same changes to two different branches. One will go through testing and then dead end and the other will go through stage to prod when the dev branch tests ok. The base branches for dev and stage are very different but the changes are the same. This time I forgot to `stash` before `commit`. So I'm glad you asked this question. – NeilG Jun 19 '22 at 10:51
4 Answers
But why? If you have a commit, it means you already have those changes applied to your files. Some, files might have been changed since the commit, but then, if you try to get a stash of that commit changes, then the stash would be the diff of your current files and the state of these files at the commit. What I am trying to say is that I can't think of a case when you would need that.
But anyway, you can get the changes of the commit, create a diff, apply it and then stash whatever was the difference.
git diff YOUR-COMMIT^ YOUR-COMMIT > stash.diff
git apply stash.diff
git commit .
git stash
You don't have to create a temporary stash.diff
file. You can simply pipe git diff
s output to git apply
.

- 11,655
- 3
- 37
- 54
-
2
-
1Valid use case: You want to rewrite your feature branch's history and change the original commit in it, however you still want a copy of it in case you need it later :). Of course you can just do a separate second commit to save the hassle but it's nice to keep commits condensed for PR reviewers. – goamn Feb 20 '20 at 00:45
-
This worked well for me, but I had a binary image in the commit which gave me an error on apply: "cannot apply binary patch to
without full index line". I found a solution here: (https://stackoverflow.com/questions/17152171/git-cannot-apply-binary-patch-without-full-index-line) – user6096790 Apr 09 '20 at 16:15 -
what this line is exactly doing? git diff YOUR-COMMIT^ YOUR-COMMIT > stash.diff – Mohak Londhe Oct 24 '20 at 04:26
-
3`YOUR-COMMIT^` is one commit before `YOUR-COMMIT`. The diff command basically showing the difference between your commit and the one before it, then saves it to the _stash.diff_ file. – Uzbekjon Oct 24 '20 at 18:01
-
A use case of mine: Our project's build system makes specific changes to circumvent Windows's long path limitation. I prefer to work locally with long path support enabled and have to make the same changes every time to our code base to disable the workaround. I'll simply create a single commit doing these changes and apply it again and again and again using your solution's first two commands. Thanks a lot! – Roland Sarrazin Aug 23 '21 at 07:39
-
Valid use case: I pressed `commit` instead of `stash` in SourceTree UI. Commit is only local, so I'll destroy it after I get stash out of it. – Nenad Dec 07 '21 at 13:13
-
1I would appreciate a little more explanation of each line. For instance, I can't understand why apply changes that are already applied will do anything in the second line. (In my case I will go to a different branch first and then apply the changes). Also, on the third line, what does `git commit .` do? Is that commit everything? – NeilG Jun 19 '22 at 10:57
-
Also, I get "unrecognized input" when trying to apply the `stash.diff`. The reason was the escape codes for coloured output. So `git diff --no-color commit^ commit` is the answer for that. – NeilG Jun 19 '22 at 11:09
Why? In my case I had a cancelled pull request and I needed to use that code as a starting point for another ticket in another branch.
OK so what I did was git cherry-pick --no-commit <your commit hash>
Then git stash push --message "wip or whatever"

- 20,319
- 26
- 127
- 154

- 579
- 4
- 9
-
2This is a great answer. I never knew about cherry pick with no commit! I just used it now with success. Someone asked me to move some pure refactoring to a separate commit. I had already pushed to remote, so edit last commit was not possible. – kevinarpe May 28 '21 at 02:10
In SourceTree:
- Right-click the commit you want to stash
- Select 'Cherry Pick'
- Deselect the option to immediately commit after a successful merge
- This will apply the changes to your local copy
- You can then stash them

- 28,994
- 18
- 176
- 206
I find myself back here again looking for answers to this problem, but this time I found something from https://gist.github.com/garyharan that helped me solve it in yet another way, which may be more suitable for some, as it seemed for me this time.
Incidentally, there's no lack of use-cases here, it seems. Maybe they're almost always after a mistake, or a roadblock somewhere, but in my case this time I thought I would be able to push on the branch I was on, but then realised I had to back out and start a new branch.
So given I have a local commit, not yet pushed to origin, and I want to "move" that commit and use it to start a new branch, the sequence is basically git reset --soft HEAD~1
followed by git stash
, then create new branch and pop the stash. If you have more (sequential) commits you can include them too.
So you're in some branch and have additional commit(s) not pushed:
$ git status
On branch <some-branch>
Your branch is ahead of 'origin/<some-branch>' by <N> commit(s).
(use "git push" to publish your local commits)
nothing to commit, working tree clean
So first "undo" the commit:
$ git reset --soft HEAD~1
If you have 3 commits included then that's:
$ git reset --soft HEAD~3
And you should now be able to see a bunch of changes ready to commit, so stash these for use in your new branch:
git stash
Create the new branch:
$ git branch <new-branch>
$ git checkout <new-branch>
Switched to branch '<new-branch>'
And now pop out the stashed changes into the new branch:
git stash pop
You should be able to see all your changes listed ready to go, but not selected, so now you can add them and commit them again, but this time in your new branch:
git add .
git commit
Now you're back where you started again, except this time you're in your new branch. You can git push -u origin <new-branch>
whenever you're ready.

- 3,886
- 2
- 22
- 30
-
This is a weird workflow :). It’s the same as cherry picking (or merging, or any other steps to create the new branch) and rebasing to squash the commits together. – AD7six Feb 02 '23 at 06:22
-
So because it's the same does that mean cherry picking and rebasing is also wierd? Or just more complicated? Or just you didn't think of doing it this way before? But is it really the same? Does cherry picking undo the commit like this way does? :) – NeilG Feb 02 '23 at 08:26
-
I see I've touched a nerve - that wasn't the intention. So, the question asked was `How can I create a GIT Stash from a Commit?` This answer does not do that but instead is an answer to a different question: [how to move commit(s) to a different branch](https://stackoverflow.com/q/1773731/761202). `Does cherry picking undo the commit like this way does? :)` the question doesn't ask to do that, but the commands to update the 'old' branch (`git reset --hard HEAD~3` in this answer) are unrelated to the commands to update the new one (`git cherry-pick` is one of multiple ways)... – AD7six Feb 02 '23 at 12:46
-
[git stash](https://git-scm.com/docs/git-stash) is used to "Stash the changes **in a dirty working directory** away". Introducing a state/need for git stash just to use it is the weird part. – AD7six Feb 02 '23 at 12:47
-
Dont worry @AD7six, I dont think I have any nerves left :) But now you're conflating everything by introducing lots of question context that wasnt relevant to our discussion. I can't answer all the permutations youve generated in just this comment. But I dont think I'm wrong to say that "it's not the same" (referring to what you said, not the question) because cherry picking doesn't undo the commit but this proposal does. Also, this *does* actually create a stash from a commit, but it does other things as well. You think this is wierd for some reason, just I'm not clear what reason that is. – NeilG Feb 03 '23 at 01:41
-
Maybe the `git` manual states things about what it thinks `git stash` is for, but I use it a lot for whatever I like. No law against it? It's like a clipboard buffer for git. If you get interrupted and you want to stash stuff why not put it there? If you are in the wrong branch, hold onto it in the stash while you figure out your branches. It keeps changes out of the history and preserves them separately to the branches so you don't have to resolve the issue immediately. It's a bit of a safety net. I'd say everyone should be familiar with `git stash`. No need to feel wierd about using it. – NeilG Feb 03 '23 at 01:45