Search code examples
gitgithubrecoverygit-stash

How to recover the lost git file after git-stash pop


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.


Solution

  • 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.