1

I have a repo that have 2 master branches master_v1 and master_v2 (these get deployed as 2 different services).

master_v2 was created from master_v1 some years ago, but development has continued i both branches ever since. Now I'm fed up with "fixing" the same things twice, once for v1 and once for v2 and now I'd like to merge these together into a single branch into separate directories but still keeping commit history so we can start extracting the common code and only do things once.

Unfortunately my basic knowledge of git is not enough and what I've tried only result in conflicts.

A simplified example

master_v1 file structure before master_v2 was created:

Api
    Controllers
        WeatherForecastController.cs
    Program.cs
    Api.csproj
Api.sln

master_v1 file structure after master_v2 was branched off:

Api
    Controllers
        WeatherForecastController.cs
        V1Controller.cs
    Program.cs
    Api.csproj
Api.sln

master_v2 file structure:

Api
    Controllers
        WeatherForecastController.cs
        V2Controller.cs
    Program.cs
    Api.csproj
Api.sln

All "common" files would also have been modified in both master_v1 and master_v2 branches.

What I want now is this structure (but not lose commit history):

v1
    Api
        Controllers
            WeatherForecastController.cs
            V1Controller.cs
        Program.cs
        Api.csproj
    Api.sln
v2
    Api
        Controllers
            WeatherForecastController.cs
            V2Controller.cs
        Program.cs
        Api.csproj
    Api.sln

What I have tried is this:

  1. create branch master_v1_for_merge from master_v1 (current branch is master_v1_for_merge):
> mkdir v1
> git mv Api v1
> git mv Api.sln v1
> git commit -m "prepare for merge"
  1. create branch master_v2_for_merge from master_v2 (current branch is master_v2_for_merge):
> mkdir v2
> git mv Api v2
> git mv Api.sln v2
> git commit -m "prepare for merge"
  1. create a branch master_merge from the master_v1 commit before master_v2 was branched off.

From here I'm basically lost.

If I merge in master_v1_for_merge into master_merge I end up with the expected:

v1
    Api
        Controllers
            WeatherForecastController.cs
            V1Controller.cs
        Program.cs
        Api.csproj
    Api.sln

But if I then try and merge in master_v2_for_merge into master_merge I get conflicts e.g. Both v1/Api/Api.csproj and v2/Api/Api.csproj has this:

<<<<<<<< HEAD:v1/Api/Api.csproj
========
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
>>>>>>>> master_v2_merge:v2/Api/Api.csproj

Because PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" /> was only added in master_v1 branch.

I don't want the v2 files to affect the v1 files or vice versa.

What are the correct git commands to use for this?

My version of git is 2.40.1.

Martin
  • 21
  • 3
  • Disable rename detection when merging – knittl May 17 '23 at 14:48
  • At first sight, the structure that you intend to have does not save you from having to fix up things twice. Unless you intend to move stuff common to both v1 and v2 out into a separate library in the long run. – j6t May 18 '23 at 11:58

1 Answers1

0

Implementing @knittl's comment :

you can give options to git merge to instruct it to not detect renames:

git merge -s recursive -X no-renames master_v1_for_merge

looking at git help merge, I can't find an easy way to do it with the default merge strategy (-s ort), perhaps someone can give an even more direct way to disable rename detection during a merge.

LeGEC
  • 46,477
  • 5
  • 57
  • 104