Sometimes I find myself in a situation where I have a project in a remote repository, synchronized with a local repository, and I have been making some messy committed changes. What makes it worse, I discover that I already pushed these commits to the remote repository.
If you find yourself facing a similar situation, you can save yourself with three steps to remove these bad commits from your local and remote repositories:
Step 1: return to the last "good" commit
git reset --hard <good_commit_hash>
Step 2: clean all uncommitted files and directories
git clean -f -d
Step 3: force push to the remote repository
git push -f
git push -f
is short for git push --force
. It forces a push when otherwise git would reject your git push
because you changed your repo history in your pushing repository. That is only half the story. Forced pushes also let you overwrite someone else's commits which have been pushed after your last pull.
That's it. Now you're back in time before everything is messed up :)