4

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!

1

4 Answers 4

8

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.

1
  • 1
    Sorry, if I want to remove d338e7a5626a763f74c2e69777ce930532fe008d then I do git reset --hard 47d81989c633c3afb3fc6aa1f319c0c0d4d3871b
    – Stan Liu
    Commented Aug 3, 2017 at 19:36
4

Yes you can rollback locally your work, then "force" push the commit

git push --force

Use it wisely and carefully!

1
  • 1
    Isn't it too late for this?
    – liam
    Commented Aug 3, 2017 at 19:33
1
$ git commit -m "accidently committed"           
$ git reset HEAD~                               
<< edit files as necessary >>                       
$ git add ...                                          
$ git commit -c ORIG_HEAD                                
0

If you want to delete a recent commit from Github, follow these steps:

  1. Copy the 2nd most recent commit hash/id just below the most recent commit that you want to delete
  2. Now open terminal, and run
    git reset --hard <commit_hash>
    git push origin <branch_name> --force
    
1
  • 1
    This is just a repeat of the previously accepted answer from a long time ago. It doesn't have any benefit.
    – Andrew B
    Commented Sep 28 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.