Assume we migrate:
- The
folder2
fromgit@bitbucket.org:myproject/myrepo.git
myrepo
│ README.md
└───folder1
└───folder2
- to
git@bitbucket.org:newrepo.git
How to do this:
-
To make it safe, clone
myrepo.git
into a new directory
mkdir myrepo-folder2-migration cd myrepo-folder2-migration git clone git@bitbucket.org:myproject/myrepo.git cd myrepo #prevent pushing into it git remote rm origin
-
Filter git history to keep only files in
folder2
git filter-branch --subdirectory-filter folder2 -- --all
-
Clean unwanted data
git reset --hard git gc --aggressive git prune git clean -fd
It should contain only the files from
folder2
in the root git directory now-
Go back and prepare to work for the newrepo
cd .. pwd #make sure you're in myrepo-folder2-migration
-
Clone the empty repository
git clone git@bitbucket.org:newrepo.git cd newrepo
-
Create a connection to local repository, we're going to pull from it
git remote add old-repo ../myrepo
-
Pull files from the old repo
git pull old-repo master --allow-unrelated-histories
-
Remove the connection with local
git remote remove old-repo
-
Push the changes to the remote origin
git push
The new remote repository should contain files from folder2
inside the root directory now.
I followed https://medium.com/@ayushya/move-directory-from-one-repository-to-another-preserving-git-history-d210fa049d4b with adjustments I found important and clear for understanding.
Top comments (3)
Didn't work, I tried twice, it adds the files at the root of the repo, just making a mess, the "folder2" is not created.
something might change, I know people that followed this and it did work
That is what it is supposed to do. Take the files from folder2 and put them at the root of the new git repository. Not sure how to make it keep folder2 itself, but you could always recreate it an move everything with
git mv files folder2/
.