Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 957
Chris Maes Avatar asked Oct 15 '25 14:10

Chris Maes


1 Answers

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
like image 161
Guildenstern Avatar answered Oct 18 '25 04:10

Guildenstern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!