In this tutorial, you will learn how to undo almost any mistake that you make while using GIT. When learning to use GIT it is likely that you will learn about push command, -f. This command is really powerful if you know what you are doing. It is also super easy to lose work if you do not know how it works. If you use the force push command, eventually you may accidentally override an important commit on a branch by accident. People make mistakes, things happen 🤷‍♂️ . This situation happened recently to a C# developer on one of my teams. Unfortunately, as no one on that team knew how to revert his mistake, he wasted a whole man-day re-doing the work. Instead of being a dinlo like that guy, I will teach you some handy commands to get your work back 🔥🔥🔥

First, when you make a mistake you will likely use the log command to try to find the missing commit. Using log will allow you to look at the branch commit history. This is done using this command:

The log command will show you the commit history of the repository. This will not show you every commit you have made on your local PC though. When you make a mistake, you will want access this local commit history. After making a mistake, instead of using log, use this command instead:

Try this on your local machine and you will notice different things will be returned compared to the result returned by log. git reflog is very helpful when recovering project history. You can recover almost anything that you have ever committed locally, regardless if you think you have accidentally deleted something using a force push. After using this command you should checkout each local commit until you find your previous good commit:

In the example above, 204eb5a9 is the SHA code of a commit. I copied this value from my PC just so you can see what it should look like, yours will be different. By using this command, you should hopefully be able to find your changes and get them back. This command has helped me recover from my mistakes pretty much every single time. Using reflog means you can be a coding legend, so do not forget that it exists! The last thing is to know about reverting a commit. Instead, of checking a commit out, you might want to reset a branch. You can do a reset with these commands:

The --hard command is the same as a force push. It will revert changes no matter what! As long as you have created a commit locally though, you can still get it back 😌. This tip might be simple, however, it can be a lifesaver. Happy Coding 🤘