Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quick cheat sheet for merging two separate repositories while retaining commit history.
# Basic process for merging two Git repositories while retaining commit history.
# - This seems to work pretty well for migrations to Pantheon
# Clone the repository that will be the final origin
git clone <repository_uri_1> <local_dir>
cd <local_dir>
# Add the second repository as a remote
git remote add <remote_name> <repository_uri_2>
git fetch <remote_name>
# Create a new branch based off the secondary repository
git branch <branch_name> <remote_name>/<remote_branch>
git checkout <branch_name>
# Perform any modifications to this branch that need to be done
# before merging into the final repository.
# Merge the secondary repository branch into the final repository and clean up.
git checkout master
git merge <branch_name>
git -D <branch_name>
git remote remove <remote_name>
Copy link

dficker commented Jun 12, 2014

The second-to-last line does not work and I think should be:
git branch -D <branch_name>

Copy link

dficker commented Feb 7, 2017

Also, as of GitHub 2.9, the final git merge command will need to be:

git merge --allow-unrelated-histories <branch_name>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment