how to merge a branch to a branch that changed a lot and were not merge for a couple years

Viewed 91

Branches:

  • dev: my main branch
  • experimental: split from dev 4 years ago.
  • feature1: split from experimental 6 months ago

Now I need feature from feature1 but I don't need other features from experimental. How to move the commits from the feature1 branch to dev but without merging it, cause there will be to much conflicts? There are ~400 commits in feature1 experimental and ~3000 difference in dev and experimental.

1 Answers

As always, let's start by visualizing the situation:

o---o---o---o---o---o---o---o---o---o---o---o---o---o (dev)
         \
          o---o---o---o---o---o---o---o (experimental)
                           \
                            A---B---C (feature1)

The easiest way to bring the changes from feature1 into dev is to use the cherry-pick command:

git switch dev
git cherry-pick A^..C

This will apply the range of commits between the parent of A (exclusive) and C (inclusive) on top of dev. In other words, commits A, B and C:

o---o---o---o---o---o---o---o---o---o---o---o---o---o---A'---B'---C' (dev)
         \
          o---o---o---o---o---o---o---o (experimental)
                           \
                            A---B---C (feature1)

Note that you're still going to have to resolve any conflicts between the changes contained in A, B and C and dev. However, it won't be as many as if you had tried to merge feature1 to dev.

Related