Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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