470

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 ?

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
YD8877
  • 10,401
  • 20
  • 64
  • 92
  • 4
    I think you are looking for this? http://stackoverflow.com/questions/556923/how-to-merge-my-local-uncommitted-changes-into-another-git-branch – zx1986 Dec 19 '14 at 02:05

3 Answers3

731

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
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • 9
    @sfletche if you want to name your stash you need to do git stash save , otherwise, like you say, it is the same as git stash. – SgtPooki Jun 18 '14 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? – Thomas Higginbotham Dec 10 '14 at 13:42
  • Just commit your changes to the new branch. – ChrisR Feb 15 '15 at 09:59
  • 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 ` obviously). I know it is not a real solution, but is the best you can do with **git**. – Kamafeather May 28 '15 at 11:17
  • I also finalized this with `git stash drop` once I'd committed – oddmeter Sep 14 '15 at 13:53
  • Use `git stash -k -u` to also include your unstaged files (and remove them from the working directory after the stash) – Marcus Junius Brutus Jul 19 '17 at 15:55
  • 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. – randomcontrol Jan 02 '20 at 12:39
  • 13
    "Is the standard procedure not working?" is a rather condescending question, when Yash was asking what the standard procedure was. – Luke Hutchison Nov 07 '20 at 23:08
  • @Kamafeather, I couldn't find `git clear` command. I think its `git clean` to clean-up the working directory. Also, when we use `git stash push` command, it automatically cleans the working directory. So there is no need to use `git clean` as far as I know. – Muhammad Tariq Jan 01 '21 at 13:46
273

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.

Community
  • 1
  • 1
Rodney G
  • 4,746
  • 1
  • 16
  • 15
  • 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. – sinisterOrange Sep 26 '14 at 00: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? – David Doria Jan 21 '16 at 23:15
  • 3
    @DavidDoria You have to commit the changes to your new branch before going back to master. – Rodney G Jan 21 '16 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 '16 at 18:39
16

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Hamed Yarandi
  • 1,085
  • 1
  • 12
  • 21