3

I did a git commit, but I just remembered that I checked out the wrong branch, I did a git stash and deleted branch

git checkout -b "myBranch"
git add .
git commit -m "message"
git stash
git branch -d "myBranch"

this is not stored, how do I restore my stash to the new branch?

git checkout -b "myBranch" dev
git stash pop
  • 1
    Nothing that was committed by `git commit -m "message"` would have been stashed. Use `git reflog` to recover and rebase your commit. https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git – Ry- Nov 15 '22 at 23:46

1 Answers1

1

Nothing is lost, it's just not where you think it is.

  1. check your reflog (git reflog) to find back the commit you created in your first command (when myBranch started at the wrong point)
  2. from your current (correct) myBranch, run :
git cherry-pick <sha of the reflog commit>

(for step 1: if you were still in the same terminal as the one where you created the "wrong" commit, you could also use the sha printed on your console when you ran git commit -m "message")

LeGEC
  • 46,477
  • 5
  • 57
  • 104