To expand @laconbass answer
Initialize example git repository
mkdir repos mkdir repos/repo-to-split || mkdir repos\repo-to-split cd repos/repo-to-split || cd repos\repo-to-split git init mkdir src mkdir src/math1 || mkdir src\math1 mkdir test mkdir test/math2 || mkdir test\math2 echo "base readme" >> readme.md echo "src/math1 readme" >> src/math1/readme.md || echo "src/math1 readme" >> src\math1\readme.md echo "test/math2 readme" >> test/math2/readme.md || echo "test/math2 readme" >> test\math2\readme.md git add . git commit -m "readme's added"
You need to
- Make sure there are no changes:
git add . && git stash
- "git subtree split branch" a directory from master/main into its own branch
# Prefer directories in branch names like features/[feature name] git subtree split --prefix=src/math1 --branch=math/src git subtree split --prefix=test/math2 --branch=math/test
- delete the directory you just created a branch for
# On windows, when you install git, select option to add unix utilities to PATH for rm rm -rf src/math rm -rf test/math git add . git commit -m "temporarily remove git subtree split folder branches (adding back in next step)"
- "git subtree add" the branch back into master/main
# Finally, *split-in* the two branches back into master git subtree add --prefix=src/math1 ../repo-to-split math/src git subtree add --prefix=test/math2 ../repo-to-split math/test git add . git commit -m "added git subtree split features back in"
Git subtree will keep a full copy of the branch in master/main
If you need to git subtree add to other remote/external repositories, that works too
Then I highly recommend creating hooks so git subtree push tries to push to the remote. Would be nice if permission failures while git subtree push would display as warnings and skip
Bonus:
cd ../../repos || cd ..\..\repos git clone --bare repo-to-split repo-to-split-worktree cd repo-to-split-worktree git worktree add master git worktree add math/src git worktree add math/test ls master || dir master la math || dir math
You now have all branches in a single repository. You can navigate into each branch/trunk added and work like normal. While having access to other branches without needing to re-clone
git subtree split. I followed the procedure in the first answer and it works like a charmgit subtree splitdoes not maintain the rename history from outside of that directory, so this approach would be as effective as just copying the files to a new repository. It’s also worth noting thatgit subtree splititself rewrites your history, generating new hashes.