0

I believe I have lost the contents of my Git Stash. Here was my workflow:

I was working on Branch A, made some changes, then did git stash. I then checked out my Master branch as I needed to fetch from origin to keep my master up to date. By mistake, I did git stash pop and git status in Master, where all the changes I made in Branch A were staged.

My critical mistake was that I did not want to include the Branch A staged changes in Master, so I checked those out. This is because I assumed that if I go back into Branch A and did git stash pop, I would be back to the latest changes.

However, I checkout out Branch A, did git stash pop, and all my changes were not there. Everything was back to the last commit.

I do understand that if I did not do git stash pop in Master, I should have got all the changes back in Branch A - so lesson learned. But as a long shot, is there any way to recover from this?

I have tried:

git stash list

But no sign of the stash with my changes. I realistically think I've lost those changes in Branch A but if there is anything I've missed I'd be grateful for any help?

Hexana
  • 1,095
  • 2
  • 12
  • 35

2 Answers2

4

I have found a solution that I thought I would like to share.

You go back to the Branch where you mistakingly checked out the files after doing git stash pop (Master in my case). You then do:

$ git fsck --unreachable

This gives you a list of the dangling blobs ...

unreachable blob 070204aa62dc0ef612f922a02d06d3....
unreachable blob 821c4ca73cb5c99f7fa5f23358e3a9....
unreachable blob b91c74fc1b5c0f8eb8ccfd1a8024c6....

I then did

git show 070204aa62dc0ef612f922a02d06d3....

This lets you view the content in the dangling blob, but does not give you the name of the file. You can then write that content to a file by using the first 5 characters of the sha1

git show f0480 > myfile

This puts 'myfile' in the directory you are currently in. Myfile at this point is a .txt which you can then copy and paste/convert to the correct MIME type (.php) in my case.

I also want to point out that I found vi's post on recovering dangling commits helpful

Hope this helps.

Community
  • 1
  • 1
Hexana
  • 1,095
  • 2
  • 12
  • 35
1

After you checked master branch and you did git stash pop there, you should again do git stash it would put all that changes into your stash again.

From what you wrote, I think that those changes should still be on your Branch A, since you just switched back from master to Branch A. Unless you destroyed them on master.

grimsock
  • 774
  • 5
  • 18