So, I love to squash commits for a feature branch when it is merged to develop
(and then eventually to master).
This works well if your Pull Requests (or Merge requests on Gitlab) are accepted and merged relatively quickly.
Often though, I end up in this scenario:
develop
-> on commit AfeatureOne
-> commit B-C-D added- Pull request opened for
featureOne
againstdevelop
- Need to start working on
featureTwo
branch which depends onfeatureOne
featureTwo
-> commit E-F-G added- Pull request for
featureOne
againstdevelop
gets merged and squashed develop
-> commit A-H- Now, if I try to rebase
featureTwo
withdevelop
, I get conflicts about commits B-C-D since git is comparing the commits fromA
as the only common parent.
I can't believe I had not tried to find an elegant solution to dealing with this (or maybe I did and had forgotten).
This useful article on git rebase --onto
helped find the solution.
Do a git log
on featureTwo
, and find the number of commits that were added after D. In our case, this number is 3.
Run:
(on featureTwo branch)
git rebase --onto develop HEAD~3
This ensures that only the commits E-F-G are replayed on top of develop. And avoids unnecessary merge conflicts!