Search code examples
gitgit-rebase

Git rebase: fixup multiple commits with one commit


Suppose I have a git branch and in final review before sharing it, I find a bunch of small errors, like spelling errors, for example. What I would like to do is apply that one patch as a "fixup", but have it actually affect many commits so there is no sign of the errors in the final history.

That is, if I change line A in one commit then change line B in another commit and then I have a patch that affects both line A and B, I'd like to do a rebase where the commit changing line A gets fixed by the A part of that patch and the commit changing line B gets the B part without manually figuring out which commits those are. Is there an easy way to do this?

I think the pseudocode script would be something like:

collect all hunks from the fixup
for commit in the history we are rebasing:
    check out commit
    for hunk in fixups:
        try:
            apply hunk to the working tree
        except:
            continue
        remove hunk from fixups
    commit the working tree.

Solution

  • It appears that git rebase --autosquash provides most of this functionality: If you have a commit that starts with fixup! then git rebase --autosquash will reorder that commit and make it a fixup, merging it in.

    Furthermore, this seems to provide the rest of the functionality I was asking about!: https://github.com/torbiak/git-autofixup

    With git autofixup, it takes your working changes, finds which parts of it go with what, and makes potentially several fixup! commits automatically. So...

    1. Make fixup changes.
    2. Run git autofixup master and it'll take the working changes and make one or more fixup! commits automatically.
    3. Finally do git rebase --autosquash master and the fixup! commits will be reordered into place.