Has it ever happened to you that you have woken up😰 from deep sleep with the nightmare that the code on which you have been working on for the past 2 weeks has vanished in thin air?
That in the dream, you accidentally performed a
git reset --hard origin/master
on a branch and later realized you were supposed to do that on a different branch & now all you want to do is go to the washroom and cry yourself out?
If answers to the above question is 'yes', then your life is going to change today. (Dramatic music plays 🎶)
Git provides a very not-so-known command that allows you a high level of control over your git repo than you could ever imagine. That command, ladies and gentlemen, is (🥁🥁Drumroll🥁🥁)
Git reflog is amazing with respect to the ways in which it helps you in recovering your project history. You can recover almost anything that you had committed at some point of time in the past using the reflog command.
Here is an example of the command output:
Now, if you want to take your code base to the state it was at time time of any of these commits, just do
git reset --hard <sha-id>
Else, if you just wanted to 'borrow' the code from any one of those commits, do a:
git cherry-pick <sha-id>
And that's how you sleep🛌 peacefully at night henceforth.
If you liked that, Check out other content of mine that got moderately famous: