Skip to content

Instantly share code, notes, and snippets.

@sambrightman
Created August 21, 2018 09:46
Show Gist options
  • Save sambrightman/f6f839be6492fc118e191e6a79f7ca3d to your computer and use it in GitHub Desktop.
Save sambrightman/f6f839be6492fc118e191e6a79f7ca3d to your computer and use it in GitHub Desktop.
Split a Git directory into a different repo with history
# there are probably more efficient ways to do this, but this seems clear and light on low-level commands
# it also does all operations locally until ready to push
cd /path/to/orig_repo
# split off directory into a branch
git subtree split --prefix path/to/subdirectory --branch subdirectory-split
cd /path/to/new_repo
# merge the branch into the new repo
git remote add orig_repo-local /path/to/orig_repo
git fetch orig_repo-local subdirectory-split
# git merge --allow-unrelated-histories orig_repo-local/subdirectory-split
# however, subtree split & merge doesn't seem to allow merging to the, or any, prefix directory
# so this low-level nonsense is required to put the subtree back in the right place
git merge --allow-unrelated-histories -s ours --no-commit orig_repo-local/subdirectory-split
git read-tree --prefix path/to/subdirectory -u orig_repo-local/subdirectory-split
git commit -m 'Merge subdirectory from orig_repo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment