3

I'm having some troubles right now I am using git flow and created a feature called feature/newbranch and I was working on develop. I wanted to stash my changes and save them to feature/newbranch so I thought all I had to do was:

git stash  (in develop)
git checkout 'feature/newbranch'
git stash pop

This puts changes in my newbranch but when I move to develop

git checkout develop

All of my stashed changes are still there.

Am I doing something wrong?

mkral
  • 4,065
  • 4
  • 28
  • 53

3 Answers3

3

Nothing is put into a branch until you commit it. What you are doing here is putting the contents of the stash into the working tree.

You should get better idea by reading top answer to following question: Difference between HEAD / Working Tree / Index in Git

Community
  • 1
  • 1
Michał Szajbe
  • 8,830
  • 3
  • 33
  • 39
  • Yea thanks, it seems like this is where I was misunderstanding how it worked. I just got lucky before and always committed without understanding the Working Tree. Thank you! – mkral Sep 14 '12 at 23:07
2

Uncommitted changes only exist in the working directory and are not bound to a specific branch. If you want to apply stashed changes to a branch, use git stash apply instead of git stash pop. git stash pop will automatically discard the most recent stash, while git stash apply only applies it, but does not delete it.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks, that makes a lot of sense but I wasn't having a problem with the pop vs apply. I just needed to commit my changes. I guess I understood git less than I thought I did. – mkral Sep 14 '12 at 23:05
  • @mkral: You wrote that you only wanted to stash them and not commit them. If you want to keep the stash, just don't run the `git stash pop` command. Am I missing something? – knittl Sep 14 '12 at 23:07
  • No, you are correct about that. I worded the question poorly by emphasizing `no commits`. However that's not what I intended due to my misunderstanding of git. I will edit the question to exclude that bit. Sorry for the misunderstanding. – mkral Sep 14 '12 at 23:11
2

No, git stash pop only tries to apply the patch/changes that you saved on the last time you did git stash . It won't commit any changes to the current branch. And if you dirty you working tree, it will stay dirty even when you switch branch. Sometimes you won't even be able to switch branches due a dirt working tree.

To clean your current working tree before switch branches , you should do a git reset --hard

André Oriani
  • 3,553
  • 22
  • 29
  • Ok thanks, I guess that make complete sense. I basically didn't commit the files otherwise it would have worked if I needed to switch back to develop? – mkral Sep 14 '12 at 23:02
  • yes, you have committed that would create a new commit in development, allowing to switch to last commit of `feature/newbranch` – André Oriani Sep 14 '12 at 23:05