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.
Save your local changes first somewhere on the side ( backup )
You can browse your recent commits, then select a commit hash by clicking on "Copy the full SHA" button to send it to the clipboard.
If your last commit hash is, let's say g0834hg304gh3084gh ( for example )
You have to run:
git push origin +g0834hg304gh3084gh:master
Using the hash that you've copied earlier to make it the "HEAD" revision.
Add your desired local changes. Done ;)
Use
git revert
for reverting your push.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).
This work for me.
git log
to find out the commit you want to revertgit push origin +7f6d03:master
while 7f6d03 is the commit before the wrongly pushed commit.+
was forforce push
And that's it.
Here is a very good guide that solves your problem, easy and simple!
Find the ref spec of the commit you want to be the head of your branch on Github and use the following command:
git push origin +[ref]:[branchName]
In your case, if you just want to go back one commit, find the beginning of the ref for that commit, say for example it is 7f6d03, and the name of the branch you want to change, say for example it is master, and do the following:
git push origin +7f6d03:master
The plus character is interpreted as
--force
, which will be necessary since you are rewriting history.Note that any time you
--force
a commit you could potentially rewrite other peoples' history who merge your branch. However, if you catch the problem quickly (before anyone else merges your branch), you won't have any issues.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:
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):
5 - Get back the stored changes to local repository:
7 - In case you have untracked/new files in the changes, you need to add them to git before committing:
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:
or