gitgithub

How to remove a pushed commit on GitHub?


As you can probably guess from the title, I accidentally pushed a commit. I was wondering if there is still a way for me to delete it or remove it?

Although this question has already been asked (like here) I thought it would be useful to add a more updated version to the site as I am not sure whether the answer provided there still applies today.

Thanks in advance!


Solution

  • Yes, assuming the commit you want to remove is the most recent commit. You can get the commit ID of the commit before the commit you want to remove. You can do this with git log. It might look like something like this.

    commit d338e7a5626a763f74c2e69777ce930532fe008d
    Author: Stan Liu 
    Date:   Wed Aug 2 18:35:00 2017 -0700
    
        Commit 2
    
    commit 47d81989c633c3afb3fc6aa1f319c0c0d4d3871b
    Author: Stan Liu
    Date:   Wed Aug 2 18:31:39 2017 -0700
    
        Commit 1
    

    Then do git reset --hard <COMMIT_ID> if you want to completely remove the changes on your local machine. In my case I will remove d338e7a5626a763f74c2e69777ce930532fe008d with git reset --hard 47d81989c633c3afb3fc6aa1f319c0c0d4d3871b. When you want to completely remove the last commit, you need to mention the commit id of the one before last. Then push your new branch up to the remote repo if you have a remote.

    If you just want to remove the commit but keep the changes of that commit, then git reset <COMMIT_ID> will work.