Revert last pushed Git Commit
Many times we push some commit to GitHub which is required to be reverted due to whatever reason.
There are high chances that you might be facing this issue regarding How to undo the last commit?
You need not worry I have got you covered, there could be other ways as well, but this one I have been using for quite some time.
First, we will start by making pushing a commit to the Github repo.
We already saw that there is a commit as "first commit". Now we make some changes and commit again with the commit message "second commit".
// Make changes in any of the file > git add . > git commit -m "second commit" > git push origin master
Here we can see we have a second commit with the message "second commit" pushed in Github.
Now we want to revert this commit to the first commit. First, we will check the logs using the following command.
> git logs commit 12c2b0d3e7378c8d4e0ab916b89e729ab96ce056 (HEAD -> master, origin/master) Author: Apoorv Tomar <apoorvtomar@Apoorvs-MacBook-Pro-M1.local> Date: Wed Apr 6 22:13:30 2022 +0530 second commit commit 78333de1b46172e05b4c3cce8991aa08e18985d2 Author: Apoorv Tomar <apoorvtomar@Apoorvs-MacBook-Pro-M1.local> Date: Wed Apr 6 20:50:22 2022 +0530 first commit > git reset 78333de1b46172e05b4c3cce8991aa08e18985d2 > git reset --hard > git push -f origin master
By using these commands you will be able to reset any of the older commits. You can reset the code by git reset --hard as most of the files which you have committed would be unstaged. By force pushing the code using "-f" flag, we will be able to override the older second commit which we have pushed earlier.
Video Tutorial Link
A video tutorial explaining the above concept.
About The Author
Apoorv Tomar is a Software developer and part of Mindroast. You can connect with him on Twitter, Linkedin, Telegram and Instagram. Subscribe to the newsletter for the latest curated content. Don’t hesitate to say ‘Hi’ on any platform, just stating a reference of where did you find my profile.