minimize git diff by undoing trivial changes

When working with code (mostly in my case) and specifically with git and gitlab, I often find myself working on a specific merge request and feature addition for several weeks. At the end, I arrive with a very long merge request that is very hard for the maintainers to understand, because I have committed a lot of changes.

Some of these changes are intentional and important to the feature at hand, others are trivial like fixing the indentation of a certain section of code, which I often to to improve readability while I'm debugging. However, in order for the MR to be as small as readable as possible, I'd like to "undo" all the trivial changes not affecting the code itself (but only the layout) before removing the WIP label from my MR. So I sometimes find myself going through my MR and undoing all those prettifications by hand in order to make the MR more readable for the reviewers.

This is a lot of stupid work that could be spent better elswhere.

Is there a script or mechanism that I can use (specifically on code) to go through the code and undo all trivial changes (for example, whitespace changes) with respect to a certain commit? This would simplify my life significantly. I could see myself writing a script for this, but I'm hoping for there to be some git magic that I can use, or for someone else already having solved this issue for me. Any suggestions?

1 answer

  • answered 2019-05-21 09:04 Suma

    It is better to plan for this and keep commits for the changes you intend to go into the merge and those which are only temporary for you separate. Preparing the merge is then as simple as cherry picking those important commits into a new branch, which is the merged into the main / master, like this:

    Working branch

    • Create a working branch
    • Commit 1: Change indentation
    • Commit 2: Implement something
    • Commit 3: Add debugging logs
    • Commit 4: Improve the implementation
    • Commit 5: Remove some of the logs

    Ready to merge

    • Create a MR branch
    • Cherry pick commits 2 and 4
    • Merge into the master

    Even if you did not plan for this and have some commits in your working branch where you mixed the temporary and permanent stuff, you can select only some parts of the commit during cherry picking into the "ready to merge" branch. How pleasant this will be may depend on your git tools. I use IntelliJ IDE Git integration, which makes tasks like this quite easy.