Un Ltd. web logs by this guy!

Tips: Merging two separate Git Repos

2017-10-16

     

Please learn Git! ASAP..

Do check out my past post (Primer: Gitting Started!) on where to get started with git.

Now to merging Two Repos

Perhaps not an often faced problem, but it can be a little tricky. So my use case was that I had a remote repo that had some legacy code and I have the newer cut sitting on my local machine. The nostalgic fellow that I am, I did not want to lose any history (which I will probably never revert to, but its good to see where we have come from) Thus came the need for doing a git merge with a third merger repo :blush: and here are the steps

Step 1: Init the Third repo locally

You may need to commit some file first to the Third merger git repo the repo started: I recommend the {.gitignore} file from your new repo.

git init ThirdRepo
cp UpgradedLocalRepo/.gitignore ThirdRepo/
cd ThirdRepo
git add .
git commit -m "INIT: the Final destination repo"

Step 2: Add the old remote (legacy) and pull the content

git remote add RemoteRepo <LegacyRemoteURL>
git pull RemoteRepo master

Step 3: Merge with the Third repo

git merge RemoteRepo/master
git commit -m "OLD_MERGE: successfully imported the old repo trees"

Step 4: Handle the old code and commit the changes

You may either get rid of unnecessary files or back them up in a different directory

git rm <all files you dont want>
git commit -m "HANDLED_STATE: taken care of the legacy code"

Step 5: Now add the new local repo (upgrade) and pull the content (like Step 2)

git remote add LocalRepo ../UpgradedLocalRepo
git pull LocalRepo

Step 6: Merge with the Third repo (like Step 3)

git merge LocalRepo/master
git commit -m "NEW_MERGE: successfully imported the new repo trees"

Step 7: Finally, the push

Now that the histories from both the git repos are roots of the Third merger repo, make all the necessary changes to keep or delete the backed up data from the legacy repo here And once we push our changes to the remote.. and Voila! you have now married your old history to your new one.. and your third repo will continue happily ever after! :smile:

# once you are done with all your changes and the ThirdRepo is your final expected state, then
git push RemoteRepo

TODO: Screencaps will be added on request.. feel free to leave a comment below if you would like some

Note:

Some of the Stachoverflow posts I saw on the same were not addressing the exact problem statement I was trying to address. Also, I came across a pretty good post by Eric Lee on the same.


Similar Posts


Comments