gitgit-rebasegit-rewrite-historygit-squash

git rebase squash take second message (like fixup)


Suppose I have some commits:

<sha1> bug due to function1
<sha2> bug due to function2
... other commits

and I would like to squash commits 1 and 2 together, keeping only the message of the second commit, then I would use git rebase -i, edit to:

pick <sha1> bug due to function1
squash <sha2> bug due to function2
... other commits

And I would always need to edit the combined messages and delete the first one.

I know I could rearrange the commits and use fixup like this:

pick <sha2> bug due to function2
fixup <sha1> bug due to function1
pick <sha3> other commit

but then I have the risk that, reversing the order of the two commits, there might be some conflicts.

How could I achieve the same result with less manipulations, especially avoiding the editing of the combined message. Note that there might be many commits before commit 1 and after commit 2.


Solution

  • Use fixup -C in the rebase todo editor. [1]

    pick <sha1> bug due to function1
    fixup -C <sha2> bug due to function2
    ... other commits
    

    Documented in the editor session.

    # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
    #                    commit's log message, unless -C is used, in which case
    #                    keep only this commit's message; -c is same as -C but
    #                    opens the editor
    

    Quirks

    Note that the squash commit will get the author and timestamp from <sha1>, not from <sha2> (thanks to Johannes in the comments).

    Notes

    1. Since Git v2.32.0