0

I had 2 branches. I recently did git stash on first branch and then moved to different branch. I made few changes in the second branch. And i wanted to move to the first branch so I did git stash again in my second branch and moved to first branch. But after i did git stash pop it merged few files and i lost all my changes after the previous commit. The got the following message after git stash pop

git stash pop Auto-merging src/settings.jsx CONFLICT (content): Merge conflict in src/settings.jsx Auto-merging public/css/index.css

I expect to recover all the files that i lost after git stash pop I'm new to git. Any help would be mch appreciated. Thanks in advance.

Neel Dsouza
  • 1,342
  • 4
  • 15
  • 33
  • Can you do git status and see which files are present? – Let's_Create Jul 03 '19 at 07:06
  • Are you sure you "popped" the right stash state .... do a `git stash list` to see the sorting of your stashes. You maybe applied a wrong stash state to your branch. You may apply it then with e.g., `git stash apply stash@{2}`. `git stash pop` just pops the latest stashed state ... if you stashed on the other branch, maybe you applied the wrong one. – YesThatIsMyName Jul 03 '19 at 07:08
  • To undo/restore changes you may have a look at `git reflog`. – YesThatIsMyName Jul 03 '19 at 07:18

1 Answers1

0

Pop will simply return you only last git stash files. So here is what you should do-

  1. Get the list of all stashes :
$ git stash list

It will return you something like :

stash@{0}: WIP on develop: 049d071 added the index file
stash@{1}: WIP on develop: j264053 Revert "added file_size"
stash@{2}: WIP on develop: 21t80a4 added number to log
  1. Now you can apply whichever stash you want to apply:
git stash apply stash@{2}

Or

git stash apply 2

Apply will not remove your stash like pop so you can still revisit it later.

user
  • 867
  • 9
  • 21
  • I had tried git stash list and then `git stash apply stash@{1}` but it was giving me an error. I tried the following: `git stash list stash@{0}: WIP on range-slider: 7ca64b9 remove tests from commit hook stash@{1}: WIP on radiobutton: c15e246 WIP: radiobutton test-cases ` ` git stash apply stash@{1} unknown option: -encodedCommand ` – Neel Dsouza Jul 03 '19 at 08:48
  • Are you using some option (-encodedCommand) while applying git stash ? – user Jul 03 '19 at 09:05
  • No. I resolved the issue by using " `git stash apply 2` " I recovered the lost file, But a new file which i had created in a different branch got merged in this branch after `git stash apply` – Neel Dsouza Jul 03 '19 at 09:11
  • Great ! Yes you can use that too. – user Jul 03 '19 at 09:13
  • Nice, you wrote as an answer what I was commenting above, but this does not answer the question, as the question is "How to recover the lost git file after git-stash pop" – YesThatIsMyName Jul 03 '19 at 13:25