How can I remove a commit on GitHub?
What are the different ways to remove a commit on GitHub, and how do they affect your repository history? Learn whether to use git reset, git revert, or a force push depending on whether the commit is local, remote, or already shared with others.
Removing a commit on GitHub can be done in different ways depending on whether the commit is only local on your machine or already pushed to the remote repository. The method you choose also depends on whether you want to completely erase the commit from history or simply undo its changes while keeping the commit log intact.
If the commit is still local and not pushed, the easiest way is to use git reset:
git reset --hard HEAD~1 # completely removes the last commit
git reset --soft HEAD~1 # keeps changes staged but removes the commit
For commits that are already pushed to GitHub, you have two options:
git revert – Safest method. This creates a new commit that undoes the changes from the unwanted commit while keeping the history intact. Example:
git revert
git push origin main
This is the best choice when you’re working with a team.
git reset + force push – If you want to remove a commit entirely from GitHub history:
git reset --hard HEAD~1
git push origin main --force
Be careful with this option, as it rewrites history and may cause conflicts for others who already pulled the old branch.
Key Points:
- Use git revert for public/shared repos.
- Use git reset for local or private branches.
- Always confirm with your team before force pushing.
In short, choose revert for safety and collaboration, but if you need a clean history, use reset with caution.