gitazuregithubsalesforce

Merge two Salesforce SFDX repos into a single mono-repo (preserve history) using Git + Azure DevOps


Context & Background

I am dealing with two Git repos with Salesforce SFDX source:

Thought Constraints / Requirements

Proposed Target repo layout (R):

root/
  sfdx-project.json
  packages/
    p1/          # P1 (from R1)
    p2/          # P2 (from R2)
    common/      # shared metadata
  scripts/ci/
  .forceignore
  .gitignore
  azure-pipelines.yml

Plan to merge (preserve history)

git remote add r1 <R1_URL>
git fetch r1
mkdir -p packages/p1
git subtree add --prefix=packages/p1 r1/main --squash
git remote add r2 <R2_URL>
git fetch r2
mkdir -p packages/p2
git subtree add --prefix=packages/p2 r2/main --squash

What I’ve already done / tried

At this point I just want to validate my strategy as it is very crucial to put up a right plan and strategy in place before starting the hands on execution.


Solution

  • If your goal is to have both projects keep their history and end up merged into a single repository, take 1 branch from each repo.... say main and merge them, having moved the root directory of each project into a subdirectory, so that when merging, they do not take the same (FS) namespace.

    So, say that I am working on my local R1 repo... and I have origin for the central R1 repo. Add a second remote R2 that points to the central R2 repo. Fetch and now you can see R2/main.

    So, let's work

    git checkout merge-r1 R1/main
    mkdir r1 # create a directory R1 that will hold everything related to R2
    git mv ./* r1/ # that should move everything from the root of the r1 project into the subdirectory r1
    # make sure that hidden files are moved too, just in case.
    git commit -m "R1: moving everything into r1 subdirectory"
    

    Now let's do the same for R2

    git checkout -b merge-r2 R2/main
    mkdir r2 # create a directory R1 that will hold everything related to R2
    git mv ./* r2/ # that should move everything from the root of the r1 project into the subdirectory r2
    # make sure that hidden files are moved too, just in case.
    git commit -m "R2: moving everything into r2 subdirectory"
    

    Now, let's merge the history of both projects... I will stay on R2 but it could be done from r1, no problem

    git merge --allow-unrelated-histories merge-r1 -m "Merging history of R1"
    

    That should give you no problem. Now, take this branch and push it into the main branch of the repo where you want to keep it (or both)... of course, make sure along with your people understand what is going on with this approach before moving forward to push into any public branch (up to this point, everything has been made on a local private branch and has not affected any public branch).

    git push R1 @:main
    

    And now the history of both branches has been tied together into a single branch.