Merging Git Repositories and Preserving History
August 28, 2015 Leave a comment
Recently I was faced with the need to merge two Git repositories and preserve the history behind the files in each. Here is an overview of the situation:
- A "Main" project repository with a remote in a public GitHub project.
- A "Secondary" project repository with a remote in a private Visual Studio Online project.
- Both repositories contained Visual Studio / .NET / C# solutions/projects.
- I needed to move the Secondary (private) repository into the Main (public) repository.
- I wanted to preserve the history of the files in the Secondary repository.
Much advice about merging two Git repositories and preserving history can be found online. Here is a sample of what can be found:
If you scan through the content at those links, you see that there seems to be multiple ways to approach this problem.
Following is the sequence of Git commands that worked for me. I must stress that this worked for me, and may not work equally well for your situation. Proceed carefully, and be prepared to handle unexpected situations.
1) Navigate to the master branch of the Main project repository.
2) Add a remote that references the Secondary project repository. In my case, this was a reference to the Visual Studio Online remote repository.
git remote add secondaryrep <URL of secondary repository>
3) Create a new branch in the Main repository.
git branch mergebranch
4) Navigate to the new branch.
git checkout mergebranch
5) Fetch the files and metadata from the Secondary repository.
git fetch secondaryrep
6) Merge the master branch of the Secondary repository into the working branch of the Main repository.
git merge secondaryrep/master
At this point I had to stop and resolve a handful of minor errors which were specific to my situation. You may or may not encounter similar issues with your own repositories.
Specifically, there was an untracked file that initially prevented the merge operation. In this case, it was safe to simply remove that file and retry the merge.
In addition, after merging, there were a few merge conflicts in configuration files related to NuGet packages (recall that these repositories contained Visual Studio / .NET/ C# projects). It was a simple matter to edit the files indicated by Git and resolve the conflicts.
7) Prepare the files for commit.
git add .
8) Opened all projects/solutions in Visual Studio and confirm that they successfully build and pass tests.
9) Commit the files from the Secondary repository.
git commit -a -m "Added projects from secondary repository"
10) Return to the master branch of the Main repository.
git checkout master
11) Merge the branch we created for the files from the Secondary repository into the master branch of the Main repository.
git merge mergebranch
12) Push the updated master branch to the Main repository’s remote GitHub repository.
git push origin master
13) Remove the branch that had been created for the Secondary repository’s files.
git branch -d mergebranch
14) Remove the reference to the Secondary repository’s remote Visual Studio Online repository.
git remote remove secondaryrep