I have a project that includes back and front. Smth like:
myProject/back
myProject/front
I want to create another repository and move front into it with its commit story. What are my ways?
I have a project that includes back and front. Smth like:
myProject/back
myProject/front
I want to create another repository and move front into it with its commit story. What are my ways?
Ok here's in detail. Let's say your proyect is
git@github.com/Vadim/myProject.git
First of all create a new remote repo and set your origin to it. We don't want to force push all history into oblivion.
Your new repo is
git@github.com/Vadim/mySubfolderProject.git
So basically, if you want to do this you copy the old one to a new one:
cp -R myProject mySubfolderProject
Then point the new one to its brand new remote
cd mySubfolderProject
git remote ser-url origin git@github.com/Vadim/mySubfolderProject.git
Now we're safe. Let's say you just want to keep master
(tabula rasa!)
git reset --hard HEAD
git checkout master
git clean -fd
Ok, then:
git filter-branch --subdirectory-filter front HEAD
This will rewrite every commit in your history as if front
was the root folder. But wait, there are a lot which didn't interact with said subfolder. Let's keep them out.
git filter-branch --prune-empty --subdirectory-filter front HEAD
You say you want to alter every branch. Okay then, instead of just applying to head, apply it to all:
git filter-branch --prune-empty --subdirectory-filter font -- --all
(you know, to dashes to separate parameter groups)
This will leave tags apart. If you dont want them, delete them before this process, but if you want to keep them:
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter font -- --all
(in fact, not using --tag-name-filter cat
will complain)
WARNING: You said to rewrite tagged commits, but not the corresponding tag. WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag. Ref 'refs/tags/v1.3.1' was rewritten
Finally, you've got a lot of space in your .git
folder. Your branches are changed for good, but .git
remembers. Let's fix that
git gc --aggressive --prune=now
Now push to your brand new remote:
git push origin --all
And there you go.
Edit:
while this will rewrite history for all branches, it won't create them in the new remote unless you checkout locally (which will set it to track its remote counterpart). So, if you want to push also a long lost feature:
git checkout feature/old_stuff
Branch 'feature/old_stuff' set up to track remote branch 'feature/old_stuff' from 'origin'.
Switched to a new branch 'feature/old_stuff'
Said old feature is also rewriten and has front
as root.
An ulterior git push
will also create the old feature in your remote.
The easiest approach these days is to use git subtree split
to pick out the history touching that particular directory into its own series of commits and then use git subtree add
to plant it into another repository.
The only problem with the resulting history will be that if the directory in the new repo will be located at a prefix (path) different from that it had in the original repo, you might get confusing results when browsing the history of the files located in that directory.
This problem might be easily fixed with git filter-branch
on the extracted history before planting it into the new repo.
Please see this and the references it contains.