Torbjorn Zetterlund

Thu 22 2015
Image

How to merge two Github Repositories into one new master branch

by bernt & torsten

I run into an issue recently with my co-developer, we had created to repos, we needed to merge the two repos. We are using Github as our main repository, and the first step we would need to do was to find the difference between the two repos, on GitHub, it’s not possible to compare two unrelated repos.

This is what we did in the end, and it worked for us:

  1. Go to the working directory of your local repo
  2. Add a remote for the other repo and fetch it
  3. Compare using git diff

For example:

cd /path/to/repo
git remote add other URL_TO_OTHER
git fetch other
git diff other/branchname
git diff ..other/branchname  # diff in the other direction
 cd /path/to/repo git remote add other URL_TO_OTHER git fetch other git diff other/branchname git diff ..other/branchname # diff in the other direction cd path/to/project-b git remote add project-a path/to/project-a git fetch project-a git merge project-a/master  # or whichever branch you want to merge git remote remove project-a 

There are other ways to do this as well, here is another example:

# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init

# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
touch deleteme.txt
git add .
git commit -m "Initial dummy commit"

# Add a remote for and fetch the old repo
git remote add -f Repro_A 

# Merge the files from Repro_A/master into new/master
git merge Repro_A/master

# Clean up our dummy file because we don't need it any more
git rm deleteme.txt
git commit -m "Clean up initial file"

# Move the old_a repo files and folders into a subdirectory so they don't collide with the other repo coming later
mkdir Repro_A
git ls-tree -z --name-only HEAD | xargs -0 -I {} git mv {} Repro_A/ 
# Commit the move 
git commit -m "Move old_a files into subdir"

# Do the same thing for Repro_B
git remote add -f Repro_B  
git merge Repro_B/master 
mkdir Repro_B 
git ls-tree -z --name-only HEAD | xargs -0 -I {} git mv {} Repro_B/
git commit -m "Move Repro_B files into subdir"
#
# Merge the second branch into the first repo's master branch
git checkout master
git merge Repro_B

Share: