Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lost git stash changes

Tags:

git

git-stash

So here's what happened: I was on a branch 'A' and did a Git stash on that branch. Then I switched to another branch 'B'. I navigated back to Branch 'A' but did not do a Git stash pop. I switched to the master branch and then back to branch 'A'. I am trying to go a git stash pop now but cant seem to get my changes back.. I need to recover that code but whenever I do a git stash pop, my file changes are not listed. I did not commit any code.

Is there a way to recover the changes that I made? would really appreciate any help in this regards.

like image 375
nids Avatar asked Aug 27 '12 17:08

nids


People also ask

How do I get my stash changes back?

To retrieve changes out of the stash and apply them to the current branch you're on, you have two options: git stash apply STASH-NAME applies the changes and leaves a copy in the stash. git stash pop STASH-NAME applies the changes and removes the files from the stash.

Can I recover a dropped stash git?

Git Stashing Recover a dropped stashYou can replace gitk there with something like git log --graph --oneline --decorate if you prefer a nice graph on the console over a separate GUI app. Or you can use the context menu in gitk to create branches for any unreachable commits you are interested in.

Does git stash delete changes?

Re-applying your stashed changesPopping your stash removes the changes from your stash and reapplies them to your working copy. This is useful if you want to apply the same stashed changes to multiple branches.


2 Answers

We also faced the same issue. So, here is how we recovered the lost changes:

  1. Go back to branch B.

    git checkout B

  2. Use git reflog option to mange reflog information.

    git reflog --all

    Output:

    f332d5c refs/stash@{0}: WIP on B: aa1d0c1 xyz commit message

  3. Now, switch to branch A using git checkout A

  4. Finally, to recover your lost changes.

    git stash apply f332d5c

like image 68
Puneet Behl Avatar answered Sep 21 '22 02:09

Puneet Behl


Stashes should be viewable via

git stash list 

or

gitk --all 

also, git stash does not stash untracked files. If you did this and subsequently did a git checkout --force of another branch to overwrite untracked files with tracked ones in another branch, you have lost that content. The recommended way to stash is with

git stash -u 

This will prevent losses of this type.

like image 32
Adam Dymitruk Avatar answered Sep 21 '22 02:09

Adam Dymitruk