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.
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.
It is not very good to re-write the history. If we use
git revert <commit_id>
, it creates a clean reverse-commit of the said commit id.This way, the history is not re-written, instead, everyone knows that there has been a revert.
Add/remove files to get things the way you want:
Then amend the commit:
The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place
Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.
For GitHub - Reset your commits (HARD) in your local repo and create a new branch. Push new . Delete OLD branch. (Make new one as the default branch if you are deleting the master branch)
To preserve the branching and merging structure is important to use the
--preserve-merges
option when doing the rebase: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 dogit 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 agit stash apply
after.To delete the commit from the remote repository:
In order delete the commit from your local repository:
link