Skip to content

Instantly share code, notes, and snippets.

@mvneves
Created December 28, 2017 21:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mvneves/23878af4789f6a5ab05079feb8097550 to your computer and use it in GitHub Desktop.
Save mvneves/23878af4789f6a5ab05079feb8097550 to your computer and use it in GitHub Desktop.
How to split a git repository into two different ones

How to split a git repository into two different ones

These are the steps to extract a directory from a git repository (with all its history commit), create a new repository from this directory, and optionally remove it from the original one.

To the ones that are here to copy-paste code, this is an example which removes the MODULE directory from REPO repository.

Step 1: Extract the directory from the git repository

Clone the original repository:

git clone https://github.com/mvneves/REPO.git
cd REPO

Extract the directory and put it in a new branch:

git-subtree split --prefix=path/to/MODULE/ --branch=new_branch

Step 2: Create a new git repository from the extracted directory

cd ..
mkdir MODULE
cd MODULE
git init
git pull ../REPO new_branch

Step 3: Remove directory from the original repository (optional)

git filter-branch --tree-filter 'rm -rf path/to/MODULE/' --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
echo path/to/MODULE/ >> .gitignore
git add .gitignore
git commit -m 'Removing path/to/MODULE from git history'
git gc
git push origin master --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment