31

Yesterday I made some changes on the master branch but didn't commit them, today I tried to pull the master but it said I have to commit or stash my changes Please, commit your changes or stash them before you can merge. I stashed them git stash and then pulled from master git pull now I have done some changes in my code but figured out that should have done the stash and i had to commit the changes. Now what can I do to have

1) the changes from stash back

2) what I got from git pull

3) and my current changes

I found this post here but the person hadn't pulled from master, so I am not sure the answers there would work for me and cannot really risk it and try as it is on master.

Community
  • 1
  • 1
farm command
  • 653
  • 3
  • 7
  • 18

2 Answers2

43

Just use git stash pop or git stash apply. As long as the stashed changes do not conflict with what you pulled or edited, it will just work, if not you get some merge conflicts that you can resolve like when you do a merge or rebase.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • I used `git stash pop` but it didn't return all the changes just some of them, what could be the reason? – farm command Dec 16 '16 at 17:53
  • 3
    Without any error message? Cannot be. Either it can apply all changes or it fails with some error message like merge conflict and didn't pop from the stash to not risk loosing changes. You can look at `git stash list`, maybe you stashed your changes in multiple steps? If there are stashes, you can diff them as usual like `git diff stash^!` to see what is in there. – Vampire Dec 16 '16 at 17:56
  • 1
    Or maybe the changes you did were exactly the same in what you got from remote and thus you don't see them as changes anymore as they are already committed. – Vampire Dec 16 '16 at 17:56
25
$ git stash list            # see stash list(s) 
$ git stash apply           # default take the top one 'stash@{0}'
$ git stash pop             # pop = apply + drop, take the top stash changes then  delete it  

$ git stash apply stash@{1} # get back number 2 stash changes
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73