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 ?
As you can see, you first need to stash changes before you can apply them to a new branch when using git stash branch .
If you have not made the commit yet, just run git stash . This will save away all of your changes. Switch to the branch you want the changes on and run git stash pop . There are lots of uses for git stash.
Nope the you get a stack (last-in-first-out) of stashes. You push a stash to your stash-stack, then another then you pop out the 2nd then you pop out the first, etc. "No and No" is a confusing answer since the OP's first question is an either/or.
The git checkout -b <BranchName> command will create a new branch and switch to it. Moreover, this command will leave the current branch as it is and bring all uncommitted changes to the new branch.
Is the standard procedure not working?
git stash save
git branch xxx HEAD
git checkout xxx
git stash pop
Shorter:
git stash
git checkout -b xxx
git stash pop
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With