Reputation: 5264
I would like to create a new GIT stash from a commit on a branch. Is this even possible?
Upvotes: 31
Views: 21641
Reputation: 4160
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.
Upvotes: 2
Reputation: 619
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"
Upvotes: 22
Reputation: 31950
In SourceTree:
Upvotes: 5
Reputation: 11813
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
.
Upvotes: 23