Reverting git commits for pull requests

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:

  1. git checkout my-pull-request-branch
  2. git rebase -i HEAD~n, where n is the number of recent commits you want to include in interactive (-i) rebase.
  3. The resulting editor will have the n commits with the word pick in front of each. Replace pick with drop for those commits you want to discard.
  4. git push --force. This will update the PR on GitHub with just those commits you kept in step 3.