Git - Migrating code and history from one repository to another

Migrating code and history from one repository to another

Use-case: Migrating code from an old repository to a new repository. And you want to retain history along the way; to make it easier to pull changes over later.

> git remote add original ssh://git@git.forge.lmig.com:7999/pagro/renters-api-sb2.git
> git pull original feature/migrate-to-ocf --allow-unrelated-histories

It adds another remote (which I called original) to your repository. We prepped a branch over in the old application with the changes we thought may need to be pulled (feature/migrate-to-ocf). We then pulled those changes over to the OCF application with the format git pull <remote name> <branch name> This command is very similar to git pull origin main but with a different remote and branch. The flag --allow-unrelated-histories was used as the new application had some baseline files/commits that the cloudforge market place already commited. This allowed us to work through the problem. As changes were done in renters, instead of having to manually make the changes in two places; now the team can manually run git pull original develop to continue pulling in the latest changes with minimal merge conflicts and manual intervention

TLDR

If we are doing the code migration into the new repo ourselves, here is the way to retain history.

  1. Point the local repo to the new vcu remote.
    git remote set-url origin <new-repo-details>
    
  2. Pull the commits from the new remote into existing local - but allow unrelated histories. This is to merge the histories from both the old and the new repos.
    git pull origin main --allow-unrelated-histories
    
  3. If both of these work successfully, git push into the new remote.

Links to this note