Solved question about splitting git repos
-
I have no problem splitting a git repo into a new repo based on subfolder and the master branch.
This command pretty much nails it.
git filter-branch --prune-empty --subdirectory-filter YOURSUBFOLDER BRANCH # example: git filter-branch --prune-empty --subdirectory-filter PublicWebsite master
But what if I have a subfolder and multiple branches other than master?
The whole point is to get the subfolder out to a new repo and keep all the history.
Can I do this?
-
That did not work, but i found my way to the solution.
- Clone the original repository.
git clone [email protected]:Your/Repo.git
- Check if there are any branches
git branch -a
Should look like this
* master remotes/origin/Branch1 remotes/origin/Branch2 remotes/origin/HEAD -> origin/master remotes/origin/master
- Check out all branches that you want to save, and then switch back to master
git checkout Branch1 git checkout Branch2 git checkout master
- Verify you have the ones you want
git branch -a
- Should look like this
Branch1 Branch2 * master remotes/origin/Branch1 remotes/origin/Branch2 remotes/origin/HEAD -> origin/master remotes/origin/master
- Remove the remote repository
git remote rm origin
- Filter the subdirectory out that you want for all of the branches. Let's assume I have a subdirectory named
Website
.
git filter-branch --prune-empty --force --subdirectory-filter Website Branch1 git filter-branch --prune-empty --force --subdirectory-filter Website Branch2 git filter-branch --prune-empty --force --subdirectory-filter Website master
- Add the new repository as the remote
git remote add origin [email protected]:Your/NewRepo.git
- Push (with force) all branches up to the new repository
git push -f --all
- You should see
To gitlab.com:Your/NewRepo.git * [new branch] Branch1 -> Branch1 * [new branch] Branch2 -> Branch2 * [new branch] master -> master
- Link the local branches to the origin/<branch>
git branch --set-upstream-to=origin/Branch1 Branch1 git branch --set-upstream-to=origin/Branch2 Branch2 git branch --set-upstream-to=origin/master master
- You should see this for each branch.
Branch 'master' set up to track remote branch 'master' from 'origin'.
-
Basically each client is a repo project, and we want to now split all the folders out to their own discrete repo projects.
Cause, is growth and shared use. when git was first implmented there was very little collaborative use of the repositories. That has changed and things need cleaned up
Current repo folder structure
Repos/ - Client/ <--This is a repository - - Public Website/ <-- Folder in repository - - Intranet Portal/ <-- Folder in repository - - Intranet Secure/ <-- Folder in repository - Client 2/ <-- This is a repository - - etc/ <-- Folder in repository
-
rewording my google criteria a few times, I got a hit that said to use
--all
instead ofbranchname
I will be testing it out.
-
That did not work, but i found my way to the solution.
- Clone the original repository.
git clone [email protected]:Your/Repo.git
- Check if there are any branches
git branch -a
Should look like this
* master remotes/origin/Branch1 remotes/origin/Branch2 remotes/origin/HEAD -> origin/master remotes/origin/master
- Check out all branches that you want to save, and then switch back to master
git checkout Branch1 git checkout Branch2 git checkout master
- Verify you have the ones you want
git branch -a
- Should look like this
Branch1 Branch2 * master remotes/origin/Branch1 remotes/origin/Branch2 remotes/origin/HEAD -> origin/master remotes/origin/master
- Remove the remote repository
git remote rm origin
- Filter the subdirectory out that you want for all of the branches. Let's assume I have a subdirectory named
Website
.
git filter-branch --prune-empty --force --subdirectory-filter Website Branch1 git filter-branch --prune-empty --force --subdirectory-filter Website Branch2 git filter-branch --prune-empty --force --subdirectory-filter Website master
- Add the new repository as the remote
git remote add origin [email protected]:Your/NewRepo.git
- Push (with force) all branches up to the new repository
git push -f --all
- You should see
To gitlab.com:Your/NewRepo.git * [new branch] Branch1 -> Branch1 * [new branch] Branch2 -> Branch2 * [new branch] master -> master
- Link the local branches to the origin/<branch>
git branch --set-upstream-to=origin/Branch1 Branch1 git branch --set-upstream-to=origin/Branch2 Branch2 git branch --set-upstream-to=origin/master master
- You should see this for each branch.
Branch 'master' set up to track remote branch 'master' from 'origin'.
-
Maybe this helps?
https://git-scm.com/docs/git-filter-branch
To rewrite the repository to look as if foodir/ had been its project root, and discard all other history:
git filter-branch --subdirectory-filter foodir -- --all
Thus you can, e.g., turn a library subdirectory into a repository of its own. Note the -- that separates filter-branch options from revision options, and the --all to rewrite all branches and tags.
-
This post is deleted! -
Couple corrections to the solution posted above after more testing this evening.