git: rework a commited patch

Posted on October 31, 2013

Since I’m working with gerrit and following a few best practices from colleagues. Here is one I found very helpful.

Motivation

A bad commit can happen from time to time, which perhaps addresses only partially a problem. Best practice is to revert the commit, rework it until it fixes the problem. Why? That way you can grab the “right” patch which fixes the specific problem instead of searching for multiple patches and fix of fixes.

How you do it

  1. Create a branch with bad commit on top (a0eae90 is the SHA of the bad commit):
    # puts you in detached head state with a0eae90b45f9b94c3f0742df627eefd864c089c8 at the top
    git checkout a0eae90b45f9b94c3f0742df627eefd864c089c8
    # checkout new branch with the name fixup/bug_2312312 in order to work on it.
    git checkout -b fixup/bug_2312312
  2. Revert the patch (HEAD~1 == a0eae90):
    git revert a0eae90b45f9b94c3f0742df627eefd864c089c8
  3. Cherry pick the “bad” commit:
    git cherry-pick a0eae90b45f9b94c3f0742df627eefd864c089c8
  4. Unstage the cherry picked commit to work on the patch:
     git reset HEAD~1

Kudos to Simon Baird and Peter Hutterer for their help on this.