An interactive rebase can be used in this situation.
Run this command:
git rebase -i YYYY~
You will be faced with a list of commits. Delete the line with commit YYYY
. Then, save and exit.
That commit is now "deleted" locally. A better way to say what just happened is that the branch you are on has been rewritten, so all commits after where YYYY
used to be will have a new SHA1 Hash.
At this point you need to force-push since history has been rewritten. If this is on the master branch:
git push -f origin master
Beware that if others are also pulling from this branch a typical pull will not work correctly for them since the tip of that branch will not be reachable with their local copy of the branch.
They can rebase their local branch on top of the new master with:
git fetch
git rebase origin/master
If there are others on this project you should make them aware of what you are doing prior to the operation.
Edit: if you want to actually remove the commit the interactive rebase is a good option. But @alextercete's approach using revert
is superior since you don't have to re-write history.