I recently had a situation where I had created a feature branch with the intention of creating a GitHub Pull Request. I accidentally made some unrelated commits on the feature branch and pushed it to GitHub as a PR. The PR had the single commit I wanted associated with the PR, and two unrelated commits. I followed this Stack Overflow answer and did the following:
git checkout my-pull-request-branch
git rebase -i HEAD~n
, where n is the number of recent commits you want to include in interactive (-i
) rebase.pick
in front of each. Replace pick
with drop
for those commits you want to discard.git push --force
. This will update the PR on GitHub with just those commits you kept in step 3.