Skip to content

Instantly share code, notes, and snippets.

@jatin-lab49
Last active December 26, 2023 09:19
Show Gist options
  • Save jatin-lab49/f155a5325a59b77d9cc7fd94813c5e30 to your computer and use it in GitHub Desktop.
Save jatin-lab49/f155a5325a59b77d9cc7fd94813c5e30 to your computer and use it in GitHub Desktop.
TIL-Lab49/Rebase --onto to avoid dependent branch conflicts after squash merge

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:

  1. develop -> on commit A
  2. featureOne -> commit B-C-D added
  3. Pull request opened for featureOne against develop
  4. Need to start working on featureTwo branch which depends on featureOne
  5. featureTwo -> commit E-F-G added
  6. Pull request for featureOne against develop gets merged and squashed
  7. develop -> commit A-H
  8. Now, if I try to rebase featureTwo with develop, I get conflicts about commits B-C-D since git is comparing the commits from A 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment