When working on a fix or a feature, I sometimes stumble over other tiny issues that can be improved on the fly in a matter of seconds. When I do them immediatly and then commit the finished feature/fix, the commit includes more than one thing. For example "add feature X and code clean up"
or "fix bug X and improved logging"
. It would be better to split this into two commits. In case the two changes happened in the same file, I can not simply add one file, commit, add the other and then commit again. So I see the following three options:
Deliberately overlook non-related things while working on something.
Copy the file with two changes, revert it, include one change, commit, include the other change, commit again.
Do not change small unrelated things but add them to a todo list and do them later.
I do not really like all of the three options, because of the following reasons:
Code quality can suffer if one does not fix small problems. And I feel bad if I consciously miss a chance to improve something without much effort.
This increases manual work and is error prone.
This is fine for not-so-tiny todos, but adding a tiny item to a todo list and revisiting it later often takes much longer than just fixing it immediatly.
How do you handle such situations?
git add -p
a lot which lets me interactively select the parts of the files I want to commit. If the clean up is sufficiently separate, this is easy to do. If separation is more difficult, I commit the state on a temporary branch, then manually add the changes to my actual branch until there's no diff to the temporary branch. This requires a lot more work, but allows me to check that each commit works on its own.