How to Remove Pushed Commits in Git?

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 :)

About The Author
Author Avatar
Mohamed AbdelGawad

IT Automation Specialist

Hi and welcome to my blog where I like to record and document the technical issues I tackle and the solutions I apply. Hopefully, it will make someone's life easier.

I'd love to hear your thoughts...

Get in Touch.

Whether you have a question or just want to say hi, I’ll try my best to get back to you as soon as possible.