Be aware that this will create an "alternate reality" for people who have already fetched/pulled/cloned from the remote repository. But in fact, it's quite simple:
git reset HEAD^ # remove commit locally
git push origin +HEAD # force-push the new HEAD commit
If you want to still have it in your local repository and only remove it from the remote, then you can use:
git push origin +HEAD^:"$name_of_your_branch" # e.g. +HEAD^:master
Some shells interpret the ^ character. For those shells, either quote/escape or use ~:
HEAD\^
'HEAD^'
HEAD~
Answer from knittl on Stack OverflowBe aware that this will create an "alternate reality" for people who have already fetched/pulled/cloned from the remote repository. But in fact, it's quite simple:
git reset HEAD^ # remove commit locally
git push origin +HEAD # force-push the new HEAD commit
If you want to still have it in your local repository and only remove it from the remote, then you can use:
git push origin +HEAD^:"$name_of_your_branch" # e.g. +HEAD^:master
Some shells interpret the ^ character. For those shells, either quote/escape or use ~:
HEAD\^
'HEAD^'
HEAD~
If nobody has pulled it, you can probably do something like
git push remote +branch^1:remotebranch
which will forcibly update the remote branch to the last but one commit of your branch.
Hi there –
I've made a complete mess of my repo. Three branches are now in an unreliable state.
Unfortunately, much of the mess is already on GitHub.
I'd like to completely delete all commits that were made after a certain point, so that I can start over with a clean slate.
How can I do this, in a way that will also delete the problematic commits from GitHub?
Unfortunately, I'm not a very proficient Git user – so anything about rebasing, reflogs, etc. is going to lose me very quickly. Please, speak slowly and use small words :)
Thanks for any suggestions.
ETA: I found this StackOverflow answer, which recommends doing git reset --hard <last_working_commit_id> to delete the bad commits, and then using git push --force to delete them from GitHub. This sounds easy, but is there anything I should consider before using this technique?