In this blog, we will see how to delete a single or n number of commits from Github repository. Sometimes mistakenly you commit your code to github and later on you realised you made a mistake. For example: you pushed .env and if you try to push your code again hiding the file this time then anyone can see your previously pushed code from the commit history. To avoid this you need to rollback your commits. Follow the below steps to delete a single or N number of commits from github:
- Make sure you are in correct path in your VS code terminal and signed in with Github CLI.
- Run the below command.
-
To delete the latest commit
git reset --hard HEAD^ -
To delete n number of commits
git reset --hard HEAD~Nwhere N= number of commits you want to delete. Possible values for N= 1,2,3 etc
-
Finally, run the below command to reflect your changes
git push origin -f
-
-
Congratulations you have successfully deleted your commit. Open your Github repo to verify this.
If you still face any problem then put your comment in the comment section.
Happy Coding 😇