1

this should be probably very easy, but I am not able to solve it out. I introduced bug in the merge, and than I corrected it in additional commit. I would like to squash the "bug fix" commit to the merge commit so it looks like the bug was never there. (my changes are only local for now). But it seems to be impossible to squash another commit to merge commit.

Could someone tell me how this should be handled correctly? I know there is possibility to revert to state before the merge, but this way I would need to solve all the merge conflicts once again what I would like to avoid.

enter image description here

Jakub Znamenáček
  • 766
  • 1
  • 4
  • 18
  • "*But it seems to be impossible to squash another commit to merge commit*" -> It shouldn't be. How did you proceed? – Romain Valeri Jul 25 '22 at 07:53
  • I wanted to use IntelliJ IDEA as I am used to, but the squash option was gray. Than I googled it and I have found out, that it is not possible to squash another commits to merge commit. – Jakub Znamenáček Jul 25 '22 at 07:54

3 Answers3

1

from the command line :

git reset --soft HEAD~
git commit --amend
LeGEC
  • 46,477
  • 5
  • 57
  • 104
1

You should be able to amend the merge commit by soft resetting on it, and adding your last commit content:

git reset --soft @~
git add .
git commit --amend --no-edit

This assumes your merge commit itself is local (not yet pushed)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thank you, it works perfectly. I didn't have to run the `git add .` with `git reset --soft @~` all changes are staged automatically. – Jakub Znamenáček Jul 25 '22 at 08:42
  • @JakubZnamenáček Yes, if your index was already populated, no git add necessary indeed. I did not see LeGEC's answer when I published mine. – VonC Jul 25 '22 at 08:53
1

Doing this in IntelliJ is also possible. The process is exactly the same as VonC and LeGEC described already. The soft reset is available from the 'Show History' window. After resetting you open the regular commit window and check the 'Amend' checkbox on top. Please be aware, that when amending already pushed commits you need to force push afterwards.

FloWil
  • 432
  • 5
  • 15
  • 1
    Thank you FloWil, I have found out this also when I was applying the LeGEC answer. I though about this problem incorrectly, instead of resetting and then modifying the previous commit I was trying to squash those two commits together. – Jakub Znamenáček Jul 25 '22 at 08:45