Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a GIT Stash from a Commit?

Tags:

git

github

I would like to create a new GIT stash from a commit on a branch. Is this even possible?

like image 568
etoxin Avatar asked May 05 '16 22:05

etoxin


3 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 diffs output to git apply.

like image 130
Uzbekjon Avatar answered Oct 23 '22 19:10

Uzbekjon


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
like image 41
Chris Halcrow Avatar answered Oct 23 '22 21:10

Chris Halcrow


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"

like image 11
davomcdavo Avatar answered Oct 23 '22 21:10

davomcdavo