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:
- 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"
- 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"
- 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.