Sometimes when your pull request is reviewed, it turns out that you have to make a small change. For instance, you have to remove a white space. Here is a really quick way to fix it, that will not even fire your editor to change the commit message.

  1. Make sure you are on the branch of the pull request (for instance, by reading the output of git status)
  2. Edit and save your file, removing the white space.
  3. Then in your terminal, use:
    git commit --amend -a -v -C HEAD
    
    This edits (--amend) your last commit, adding all the changes made to files (-a) and reusing the last commit message (-C HEAD). Once this is done, a summary is printed (-v).
  4. You can now push your changes with:
    git push --force-with-lease
    
    We need --force-with-lease to “confirm to the server that we really want to edit the last commit”.

Caution

Two words of caution with this technique:

  • you are rewriting history, if anyone has made changes on top of yours, they will face a confusing situation when they try to merge your changes again. force-with-lease protects us somewhat, as the push will fail if someone else has pushed changes on top of ours, but it won’t help if the other person’s changes are local.
  • in most situations, commit messages (and their content), should be crafted as an easy to review progression. Abusing this trick could make commits harder to review.