Consider the following practice:
- A developer branches from
main
and creates as many commits as needed in a feature branch - Once the feature is completed, all commits are squashed and merged into the
main
branch (for instance, think of a GitHub's "Squash and merge" button)
And now this is a use case that interests me:
- Create and work on a
feature1
branch - Create a
feature2
branch starting from the last commit of thefeature1
branch (see theC
commit in the image below) - Squash and merge
feature1
intomain
(see the commitG
) - Merge this newly created commit
G
into thefeature2
branch - Continue working on the
feature2
branch
In other words, the merge of G
into the feature2
branch in step 4 looks like this:
user@host:~/repo (main)$ git checkout feature2
user@host:~/repo (feature2)$ git merge main # merge G into feature2
Often, this merge (see the commit H
) results in a number of merge conflicts.
How to completely eliminate these conflicts?
The easiest solution I can think of is the following (see the image below):
user@host:~/repo (main)$ git checkout feature1
user@host:~/repo (feature1)$ git merge main # merge G into feature1; essentially, an empty commit
user@host:~/repo (feature1)$ git checkout feature2
user@host:~/repo (feature2)$ git merge feature1 # merge G' into feature2
To put it another way, instead of merging G
into feature2
directly, we first merge G
into feature1
and then merge feature1
into feature2
.
Is there a simpler approach?
feature1
branch being deleted after it is merged to main, so we may no longer have easy access to it. This argues for rebasing being a good option. – Jesus