As I ran git reset HEAD~1 --hard
, the last one commit on the repo has been deleted completely from the repo.
“Why are stressing the word dangerous here. How’s this command so dangerous when compared with other reset commands? ”, intelligent Kumar interrupted with this question.
Well. That’s an interesting question.
The reason I’m stressing that it’s so dangerous is that, this commit has the power to delete all our changes. Remember this does not bring back the changes to either staging or working area. Running this command does not ask for any confirmation from the user.
Think of a scenario where you made a small mistake by adding a “0” next to “~1” in the above command and hit “Enter” without looking and confirming the command. So, the command you ran would be,
git reset HEAD~10 --hard
Can you imagine how dangerous it is now? You lost 10 of your most valuable commits. There’s no way to reverse this process unless you pushed the code to server. So, you have to make all your changes from the beginning again.
I could better explain this with a real scenario that happened between me and Aadhithyanath (One of my colleague).
One day Aadhi approached my help to resolve a bug. I saw there were changes in 10+ files (around 14 files). I asked him to commit those changes before proceeding. He compressed all changes together in a single commit. We started fixing the bug. We fixed one scenario where the bug has happened. But, there are other scenarios too we need to handle. We made a commit. While heading to fix other scenarios we found that our last commit containing the fix for 1st scenario was implemented in the wrong way. So, we planned to remove that commit and go ahead with the correct implementation. I ran the following commit,
git reset HEAD~2 --hard
Aadhi suddenly shouted “OH MY GOD!!!!”
“What happened? ”, I asked him.
“You deleted all my changes 😠”, replied Aadhi.
“What are you saying? ”, I was asking him by looking at the terminal what I’ve executed. Do you notice that ~2
?
Yes. I deleted the last 2 commits. To our bad luck, we deleted the commit which Aadhi has compressed his changes altogether in a single commit. It was 4 hours of his hard work. Adding up to our bad luck, we haven’t pushed that code to the remote repo. This happens often to those who type faster. I’m one among them 😜.
Do you understand how dangerous it is?
That’s the reason, I stressed the word dangerous here. Do not execute this command unless you’re 200% sure of what’s will be the result of it.