471

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 ?

1

3 Answers 3

732

Is the standard procedure not working?

  • make changes
  • git stash save
  • git branch xxx HEAD
  • git checkout xxx
  • git stash pop

Shorter:

  • make changes
  • git stash
  • git checkout -b xxx
  • git stash pop
9
  • 9
    @sfletche if you want to name your stash you need to do git stash save <name>, otherwise, like you say, it is the same as git stash.
    – SgtPooki
    Jun 18, 2014 at 14:29
  • 7
    After using this approach, it seems that if you return to the previous branch, the stashed changes have returned. Is it possible to only have the stashed changes on the new branch? Dec 10, 2014 at 13:42
  • 4
    @ThomasHigginbotham no the Working Directory is common between branches and is copied from one to another when you pass from one branch to another. For "achieving" what you want I usually make different stashes, adding useful description with the git stash save "description" command mentioned previously; and then I git clear the branch (for trashing the actual working directory) and then git stash apply stash@{my_desired_stash} in the desired branch (after switching to that with git checkout <branch> obviously). I know it is not a real solution, but is the best you can do with git. May 28, 2015 at 11:17
  • 2
    git stash save is now deprecated in favour of git stash push. It differs from "stash push" in that it cannot take pathspecs, and any non-option arguments form the message. Jan 2, 2020 at 12:39
  • 13
    "Is the standard procedure not working?" is a rather condescending question, when Yash was asking what the standard procedure was. Nov 7, 2020 at 23:08
274

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.

4
  • 5
    For single stashes this is the way to go. The stash name-reference isn't required as Git will apply latest stash, switch to new branch and apply stash in 1 command. Sep 26, 2014 at 0:33
  • @RodneyGolpe This seems to also apply the stash to 'master'? What I want to do is, from 'master', git stash, then I would have expected 'git stash branch [branchname] to apply the stash to a new branch, leaving master without the edits? Jan 21, 2016 at 23:15
  • 3
    @DavidDoria You have to commit the changes to your new branch before going back to master.
    – Rodney G
    Jan 21, 2016 at 23:37
  • Now that also committed the changes to master as well...I'm really at a loss...obviously I still don't really understand git. After soft resetting master then pulling from the remote repo now I am where I wanted to be. Master is in the state before the changes. The new branch holds the changes.
    – Ekkstein
    Mar 24, 2016 at 18:39
17

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:

  1. a new branch (starting from the commit at which the stash was originally created)
  2. move changes to this branch
  3. and remove latest stash (Like: git stash pop)

After running this command, you will want to git add the changes and to commit them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.