Reputation: 10780
I was working on master branch, made some changes and then stashed them. Now, my master is at HEAD.
But now, I want to retrieve these changes but to a new branch which branches from the HEAD version of the master branch.
How do i do this ?
Upvotes: 495
Views: 386252
Reputation: 28434
The usual, standard procedure is:
git stash save
git branch xxx HEAD
git checkout xxx
git stash pop
Shorter:
git stash
git checkout -b xxx
git stash pop
Upvotes: 771
Reputation: 1211
If you have some changes on your workspace and you want to stash them into a new branch use this command:
git stash branch branchName
It will make:
- a new branch (starting from the commit at which the stash was originally created)
- move changes to this branch
- and remove latest stash (Like: git stash pop)
After running this command, you will want to git add
the changes and to commit them.
Upvotes: 19
Reputation: 4826
Since you've already stashed your changes, all you need is this one-liner:
git stash branch <branchname> [<stash>]
From the docs (https://www.kernel.org/pub/software/scm/git/docs/git-stash.html):
Creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created, applies the changes recorded in <stash> to the new working tree and index. If that succeeds, and <stash> is a reference of the form stash@{<revision>}, it then drops the <stash>. When no <stash> is given, applies the latest one.
This is useful if the branch on which you ran git stash save has changed enough that git stash apply fails due to conflicts. Since the stash is applied on top of the commit that was HEAD at the time git stash was run, it restores the originally stashed state with no conflicts.
Upvotes: 278