How can I remove a commit on GitHub?
I "accidentally" pushed a commit to GitHub.
Is it possible to remove this commit?
I want to revert my GitHub repository as it was before this commit.
Note: please see alternative to
git rebase -iin the comments below—
git reset --soft HEAD^
First, remove the commit on your local repository. You can do this using
git rebase -i. For example, if it's your last commit, you can do
git rebase -i HEAD~2 and delete the second line within the editor window that pops up.
Then, force push to GitHub by using
git push origin +branchName --force
See Git Magic Chapter 5: Lessons of History - And Then Some for more information (i.e. if you want to remove older commits).
Oh, and if your working tree is dirty, you have to do a
git stash first, and then a
git stash apply after.
git push -f origin HEAD^:master
That should "undo" the push.
Read more... Read less...
For an easy revert if it's just a mistake (perhaps you forked a repo, then ended up pushing to the original instead of to a new one) here's another possibility:
git reset --hard 71c27777543ccfcb0376dcdd8f6777df055ef479
Obviously swap in that number for the number of the commit you want to return to.
Everything since then will be deleted once you push again. To do that, the next step would be:
git push --force
In case you like to keep the commit changes after deletion:
Note that this solution works if the commit to be removed is the last committed one.
1 - Copy the commit reference you like to go back to from the log:
2 - Reset git to the commit reference:
git reset <commit_ref>
3 - Stash/store the local changes from the wrong commit to use later after pushing to remote:
4 - Push the changes to remote repository, (-f or --force):
git push -f
5 - Get back the stored changes to local repository:
git stash apply
7 - In case you have untracked/new files in the changes, you need to add them to git before committing:
git add .
6 - 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>" <file1> <file2> ...
git commit -m "<new_commit_message>" .
git revert for reverting your push.
git-revert - Revert some existing commits
git revert [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>... git revert --continue git revert --quit git revert --abort
Revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).