Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recover lost code after stash and checkout

I seem to have misplaced (that’s my hope) dozens of hours worth of work. Here is the history of what I did to my Git repo:

  • I had worked on some code modifications that I wanted to temporarily stash and revert to the previous commit. I had ‘Branch A’ checked out at the time.
  • git stash
  • git checkout 44f7b43f355e30e15ab9081c2f8e92188277ca53 (I think this is where I went wrong – this was the previous commit I wanted)
  • git revert 44f7b43f355e30e15ab9081c2f8e92188277ca53 (Clearly I didn’t know what I was doing)
  • git checkout 44f7b43f355e30e15ab9081c2f8e92188277ca53
  • I now had my previous commit code.
  • git stash apply (I wanted to move back forward with my previous changes)
  • git stash pop (At this point I assumed I was back to where I wanted to be working on Branch A. I saw all my recent changes present again.)
  • Dozens of hours worth of work done. What I didn’t realize is that the command prompt still showed this: vscode âžś /workspaces/git/aws-shared_network-terraform/terraform (7c76f7e) indicating I wasn’t actually in Branch A.
  • ran a commit in vscode
  • ran a sync in vscode. I received an error that I needed to checkout a branch first to sync.
  • I checked out Branch A.
  • "oh poop" moment because all my work disappeared.
  • git checkout 44f7b43f355e30e15ab9081c2f8e92188277ca53 (Code was not restored)

I am now sitting at:
vscode âžś /workspaces/git/aws-shared_network-terraform/terraform (44f7b43) $

Is there a way to recover this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I found that if I run git log --reflog I see my commit from today:

commit 9746368659f680c934fe97e7c70881ae4882400f
Author: Me
Date:   Wed Apr 3 17:46:13 2024 +0000

    enable cdn redirect and cleanup

I don’t want to do anything else until someone provides direction.

>Solution :

You’re safe since you did commit your changes and they’re in the reflog. The only thing that’s missing is they’re not part of a named branch. To fix that:

  1. Check out Branch A.
  2. Run git cherry-pick 9746368659f680c934fe97e7c70881ae4882400f to add your commit to Branch A.

(Note: It won’t literally add that commit hash to the branch. It will replay the commit on the branch as if you had committed to Branch A originally. You’ll get a new commit with the same contents and commit message but different hash.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading