Git remove commit from a pushed pull request

If ever you have to remove a commit from a pushed pull request, here is a simple way to do it. Bonus: it’s not adding a commit for the removal.

First be sure to be on the good branch

git checkout target-pull-request-branch

Then have a watch at the PRs commit tab and note the commits hash you want to remove (one or more)

Replace X by that number in the following command, which will make and interactive rebase for the X last commits

git rebase -i HEAD~X

You are now going to have an interactive text view, in which all the aimed commits are preceded by the word ‘pick’

Using the view like a text editor, replace the ‘pick’ word by ‘drop’ on the lines containing the targeted commit’s hashes .

Exemple

Replaced pick with drop for commits 4aa3149 and 2031a79b:


Save and exit

In my case vim was the default text editor, so here you go escape colon w q

Push force the rebased repository

git push --force

This will remove the targeted commits and the only trace will be in the PR comments. The commit page will look like the rebased one, without the discarded commits nor a mention to them.