Disaster recovering with Git reflog

Kpicaza
PHP FAD
Published in
2 min readJul 15, 2020

In every programmer’s life, you can get some mistakes that will seem irreversible, like deleting a GIT branch before pushing it, or Git-rebase that mess around our codebase, or merge by error incorrect branch to the main branch, etc…

In this short story, I will show you how Git program gives us a tool to recover whatever state our repository’s history has been through, and how to use it.

Git reflog

Git Reflog is not a very well known command, but you’ll be glad to know how to use it when you have a disaster with your repository code, I can ensure that the git reflog command saved my workday in more than once.

This command shows a history of all events that occurred in our git repository locally. Also, it shows the reference to the code status at that moment.

Let see how it works:

git reflog

In the console output, you will see all the movements that happened in your repo, from the first commit. At this point, you can carry your code to whatever instant recorded in the relfog history. You only need to reset to the wanted pointer and create a new branch from there.

git checkout HEAD@{11}
git checkout -b before-disaster

The repository is now in a well-known state before the disaster. You can take a coffee, relax five minutes, and proceed with care to not fall again in the same trap.

Conclusion

Git reflog is a powerful command in the Git toolset. As programmers, we have to know how to use it, because it can solve a lot of mistakes in many bizarre cases.

Anyway, we have to take care of not messing up our Github repositories to maintain clean, readable, and descriptive histories.

I hope you liked this short trick. Happy Coding!!

--

--