We maintain a branch structure with a merge workflow. As a result, our commit branch hierarchy is as follows.

However, since we use the Gitlab community edition, which does not squash and rebase merge, sometimes developers forget to do one or the other and compile merges that look like this.

The usual way that I follow to restore this is to reset master until the last good commit before these merges, and then replay each merge (after the correct reboot and distribution) to combine them.
A better way would be to use the command git rebase --ontoto replay good merges after eliminating the bad merges. However, I could not find the right way to do this. What am I doing:
- Reset .
- Rebase bad commit ( -rebased) .
- - .
- .
- , , 2-3.
git rebase -ip --onto <last_good_commit>, 5, , , . - , ?