Sometimes—more specifically, after a sleepless night—you might accidentally delete a Git branch (using extreme force) without merging it into your development branch. Sound familiar? It happened to me too.
Thankfully, Git is smart and capable of recovering your "vanished" code—just another reason why I love using Git.
Here’s how I restored my deleted branch:
First, run this command to see all your commits and find the SHA-1 hash of the latest commit from the deleted branch:
git reflog
Once you have the commit hash, use the following command to check it out:
git checkout <your-lost-commit>
Now, with HEAD
pointing to your commit, create a new branch:
git checkout -b LostAndFound
And there you go—you’ve got your code back!