I was working on a bigger change in some project in some local branch (for some GitHub PR, specifically this, e.g. this latest (intermediate) branch HEAD) and this resulted in quite a lot of commits:
Now as it is (mostly) ready, I want to clean up those commits, i.e. squash commits together, use nicer commit messages. However, in this specific example, many changes belong together (all commits prefixed with better subnet logic, WIP
) and I did not knew a good way to separate things beforehand.
So the original intent was to squash all those better subnet logic ...
together.
However, now that the changes have become so big, I was thinking about splitting it up.
These commits also contain intermediate TODOs/discussions/thoughts which are later cleaned up. Sometimes also an attempt at solving some problem, which I delete in some later commit and do it in another way.
Is there a good way (or tool) to reorder these commits and automatically finding pairs (or groups) of commits which would lead to smaller changes when squashed together?
E.g. let's say some commit adds this comment:
# TODO this needs to be fixed
# maybe can do way A...
# or maybe way B...?
And then some later commit removes this comment, and insteads it adds some other comment there, or maybe some code, e.g.:
# We use way B because ...
B(...)
I would want to find such commits automatically which belong together, and when squashed together, would clean up the history.
How?
I could also write some own script/tool to do that for me.
The trivial solution is to just squash all, which would lead to the minimal amount of changes. But this trivial solution is not interesting.
You could limit it by amount of commits squashed. E.g. let's assume we exactly want to find two commits out of all the local commits in the branch which reduce the amount of changes most from all two pairs of commits. This should be simple.
Small important extension: Only allow such a commit pair if the commits can be reordered such that they can be squashed without conflict.
Maybe there are better ways?
Does such a tool/script already exist?
Not directly the question, but related, and maybe you have further advise on that:
Unfortunately, it gets more complicated than that, because many of the commits contain multiple unrelated changes (within this big "better subnet logic" topic). So to make this work, I probably need to split the commits up even more.
Maybe my workflow is also not optimal. If you have an advise how to improve my workflow, please don't hesitate to comment or put this into the answer as well. I.e. how to avoid this complicated clean up procedure afterwards.
I asked a related question recently on Reddit: How to cleanup a branch (PR) with huge number of commits.