When working with code (mostly c++ 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 c++ 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?
Use the git diff Command to Ignore Whitespaces in Git We use the git diff -w command to ignore all whitespace differences. It will ignore spaces at the beginning, middle, and end of lines. We use the git diff --ignore-space-at-eol command to ignore whitespace changes at the end of our lines.
Right click on base branch in the Git tab, and do reset. It will undo the commit and stage non-whitespaces changes for commit. Now commit and push -f (I always force push via command line). That's it, you are done!
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With