lost git stash changes

33,167

Solution 1

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.

Solution 2

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

Solution 3

Something similar happened to me. In short, check to make sure you didn't accidentally push the new files to the other branch.

Here's what happened to me: I stashed my stuff, switched from 'dev' to 'master' to do a hotfix quick. When I pushed my hotfix to master, I didn't notice that I also added my new files that were meant for dev to the master branch--I too assumed stash included those files.

Share:
33,167

Related videos on Youtube

nids
Author by

nids

Updated on July 31, 2020

Comments

  • nids
    nids over 3 years

    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.

    • JamesOR
      JamesOR over 11 years
      try "git stash list" to see what stashes you have available.
  • Melon Bread
    Melon Bread almost 8 years
    Had access to commit, but lost my stash. Recovered my work git stash apply. Thanks!
  • LittleLittleQ
    LittleLittleQ almost 7 years
    @Mitkins thanks so much, I've also encountered this and git stash apply works for me!
  • Legato
    Legato over 5 years
    after git stash list you can apply specific stashes by index number. For example git stash apply 1 will apply the line that starts with stash@{1}
  • trdavidson
    trdavidson about 5 years
    you, frankly, just saved my last 2 weeks in combination with effectif.com/git/recovering-lost-git-commits. THANKS!
  • Hayk Mkrtchyan
    Hayk Mkrtchyan almost 4 years
    You saved my project))
  • Darren
    Darren over 2 years
    I managed to lose my stash, TimeMachine wasn't enabled on my new Mac. Remote backup was no help. Who knew you could get a lost stash back. Thank you.
  • Kinjal Rathod
    Kinjal Rathod about 2 years
    What id I dont find my changes in the stash list?
  • Craig  Hicks
    Craig Hicks almost 2 years
    git stash from branch main followed by git stash branch newbranch followed by git checkout main caused my stash to vanish. The checkout did not use --force. Maybe that is a git bug or limitation?