6

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?

L. F.
  • 19,445
  • 8
  • 48
  • 82
carsten
  • 1,315
  • 2
  • 13
  • 27

1 Answers1

5

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.

Suma
  • 33,181
  • 16
  • 123
  • 191
  • I would have suggested something similar. Carry on doing your changes in your branch, separate your commits (atomic commits) and when you are ready to create a MR, go through your commits and in a separate branch, based on master, cherry pick the commits you want (the clean, side improvements, so on), have this merged in master and then rebase your main feature branch on top of master, which will leave only the feature-relevant commits. – padawin May 21 '19 at 09:24
  • Alternatively you can `git rebase -i ` on your working branch. – felideon Jun 10 '19 at 20:04