Reputation: 2014
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!
Upvotes: 4
Views: 33275
Reputation: 11
If you want to delete a recent commit from Github, follow these steps:
git reset --hard <commit_hash>
git push origin <branch_name> --force
Upvotes: 0
Reputation: 210
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.
Upvotes: 8
Reputation: 494
$ git commit -m "accidently committed"
$ git reset HEAD~
<< edit files as necessary >>
$ git add ...
$ git commit -c ORIG_HEAD
Upvotes: 1
Reputation: 4753
Yes you can rollback locally your work, then "force" push the commit
git push --force
Use it wisely and carefully!
Upvotes: 4