Removing a Commit from a Remote Git Repository

––– views

1 min read

It happens to the best of us, there are many occasions when you need to remove a commit from a remote branch, you just need to be very careful doing this. I do it the following way

I first get to the target branch by running git checkout target-branch-name or if the branch is not available locally first run git fetch origin target-branch-name then checkout.

💡
Run git pull origin target-branch-name to make sure that you have the latest commits

Then run git log to list all current commits, if the latest commit you see is the one you want to remove run git reset HEAD^ —soft this will remove the commit, and you will see the changes available locally.

All you need to do now is run git push origin target-branch-name —force this will force push to your branch and remove the commit you now have as local changes.

💡
If you want to keep your changes, you could do git checkout -b new-target-branch, after that add the changes and commit them

That’s my process of doing it, I find it very useful when something is working locally but not on staging/production, so I push a bunch of commits in a row, and when I finally get it right, I git reset HEAD^ —soft all the commits and push it all in just one commit

To comment please authenticate