Skip to content

Instantly share code, notes, and snippets.

@baodrate
Created July 22, 2015 14:24
Show Gist options
  • Save baodrate/72b3d557c6ef86a00e8f to your computer and use it in GitHub Desktop.
Save baodrate/72b3d557c6ef86a00e8f to your computer and use it in GitHub Desktop.
merge a subdirectory from another repo while preserving history
# credit: http://jrsmith3.github.io/merging-a-subdirectory-from-another-repo-via-git-subtree.html
# Clone the target repo
git clone [email protected]:jclouds/jclouds.git
cd jclouds
# Add the source repository as a remote, and perform the initial fetch.
git remote add -f sourcerepo [email protected]:jclouds/jclouds-labs-openstack.git
# Create a branch based on the source repositories' branch that contains the state you want to copy.
git checkout -b staging-branch sourcerepo/master
# Here's where the two approaches diverge.
# Create a synthetic branch using the commits in `/openstack-glance/` from `sourcerepo`
git subtree split -P openstack-glance -b openstack-glance
# Checkout `master` and add the new `openstack-glance` branch into `/apis/openstack-glance/`. At this point, the desired result will have been achieved.
git checkout master
git subtree add -P apis/openstack-glance openstack-glance
# Clean up by removing the commits from the `openstack-glance` branch and the `sourcerepo` remote.
git branch -D openstack-glance staging-branch
git remote rm sourcerepo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment