Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimize git diff by undoing trivial changes

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?

like image 216
carsten Avatar asked May 21 '19 08:05

carsten


People also ask

How to ignore whitespace in Git diff?

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.

How to get rid of whitespace changes Git?

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!


Video Answer


1 Answers

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.

like image 165
Suma Avatar answered Oct 18 '22 19:10

Suma