git: best strategy to merge secondary branch

Viewed 29

Sometimes, when I am developing a big feature, I create a secondary branch (out of my original feature branch) to make it easier for my colleagues (and for myself sometimes) to visualize differences between parts of the same feature. It should look like this:

main      --o--o--o---------
                \
feature          o--o--o--o--o
                        \
secondary                o--o--o

When I need to merge everything, I usually merge secondary on feature and then to main, which works just fine. The problem is that sometimes I need to "merge by steps", meaning that I cannot merge secondary into *feature, but I have to merge feature into main, first. In that case, when I update secondary with everything new in main, it messes it up a bit, like having conflict in most files modified on both feature and secondary.

What should be the best approach to not mess this up?

P.D.: this may be a common question, but searching I couldn't put it to words to find a similar question :(

1 Answers

You should rebase the secondary branch in your then-feature-merged main branch.

In that way you will be kept with only modifications done on you secondary branch which are not already in the feature or main branches.

You could still have conflict, but only on modifications done specifically in your secondary branch so it should be easy to fix.

Summary:

  • git switch main
  • git merge feature
  • git switch secondary
  • git rebase main + any commands to solve conflit and ends rebase
  • git switch main
  • git merge secondary (add --no-ff if you want to keep a merge commit)
Related