How to Remove a Commit on GitHub

Quick Summary

  • Reset the local branch to the commit before the unwanted one with git reset, then overwrite the GitHub branch using git push -f.
  • git reset without options keeps the removed commit's changes, so stash them, force push, then git stash apply, add new files and commit again.
  • If others may have pulled the commit, use git revert instead, and prefer git push --force-with-lease over -f when force pushing.

To remove a commit you have already pushed to GitHub, reset your local branch to the commit before it with git reset, then overwrite the branch on GitHub with a force push (git push -f). The steps below also keep the changes from the removed commit, so you can fix them and commit again.

A force push rewrites the branch’s history. If other people may already have pulled the commit, use git revert <commit_ref> instead: it adds a new commit that undoes the old one and leaves the history intact. git push --force-with-lease is also safer than -f, because it refuses to overwrite commits on GitHub that you haven’t fetched yet.

Remove the commit and keep its changes

1 – Copy the reference (hash) of the commit you want to go back to from the log:

git log

2 – Reset git to the commit reference:

git reset <commit_ref>

Replace <commit_ref> with the hash you copied. Without options, git reset moves the branch back and leaves the removed commit’s changes in your files.

3 – Stash/store the local changes from the wrong commit to use later after pushing to remote:

git stash

git stash only stores changes to files Git already tracks. Files that the removed commit added are untracked after the reset, so they simply stay in your folder until step 6.

4 – Force push to the remote repository (-f or --force) so GitHub drops the removed commit:

git push -f

5 – Get back the stored changes to local repository:

git stash apply

6 – In case you have untracked/new files in the changes, you need to add them to git before committing:

git add .

7 – Add whatever extra changes you need, then commit the needed files (or use a dot ‘.’ instead of stating each file name to commit all files in the local repository):

git commit -m "\new_commit_message\" ...

8 – Push the new commit to GitHub:

git push

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)