Ever needed to delete a commit completely in GitHub but tried hundred of thousands of tutorials and still does not work? You are in luck! In this tutorial, I am going to teach you guys how to make a GitHub commit disappear forever. Note: before you carry on with this tutorial, make sure you do not have any local changes or stash all your local changes to be safe.

Follow this tutorial in order to delete one or more commits in GitHub. First, you use the following command to list all or some of your latest GitHub commits.

  git log -n 3 # 3 means to list the latest 3 commits

It should look something similar to this,

git log top 3 commits

Say you want to delete the first latest commit, you would need to type in this command.

  git rebase -i 77d55bd72c63d43cc83f9c0fea4990c33427c2a6

As you can see, instead of putting the deleted commit hash, we put the next next one. We will explain why in the next step.

Once the git bash command is entered, you should see something similar to this.

git bash command

Simply move your cursor to the line or commit that you would like to delete, and press d to delete that line/commit. Just like any other vim, Shift + colon with wq + enter to leave the text editor. You should see the response like “Successfully rebased and updated refs/heads/master.”

Okay, the reason why we did not put the commit hash for the git rebase command to the one we wanted to delete is that the text editor or rebase does not allow us to “remove everything, as the rebase will be aborted.” (this means that at least one needs to stay)

Now, you can simply commit your changes to GitHub to make your commit goes away by doing this command,

  git push -f origin master

As you can see, we need that -f option to force the Git to commit the changes, otherwise, it will not allow you to do that.

Tada. You have done it! You should head over to your GitHub repo, and the commit you deleted in that text editor should be gone now.

Wrapping Up

As this is a fairly short tutorial. Hopefully you have learned how to delete a commit completely in GitHub.

Thanks for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started