Recently, I was faced with the task of transferring a project folder from one repository to another on github. It sounds primitive, but if we consider what is given and what needs to be obtained, some nuances may arise.
So, what is given:
What needs to be done:
In theory, one could simply copy the entire repository with all its contents to a new place, and then simply delete those folders that are not needed. But this method is rather non-optimal and I didn’t particularly like it, so I decided to do otherwise.
I used the standard filter-branch git one. I took the following articles as a basis:
In this post I want to adapt the process a little for better perception.
Suppose, for example, that our repository is called movement-example
, and the only folder that we want to transfer to a separate repository is folder-to-move
. Then the steps that need to be done to prepare the transfer are as follows:
git clone git@github.com:<user_or_organization>/movement-example.git
git clone <path-to-movement-example>
is much faster (thanks to ZyXI for the hint).cd movement-example
git remote rm origin
git filter-branch --subdirectory-filter folder-to-move -- --all
folder-to-move
will remain in your local repository, and the folder itself will no longer exist - all files contained in it now lie in the current directory (in movement_example
).mkdir folder-to-move
mv * folder-to-move
git add .
git commit -m “Preparing to extract folder”
The first part is ready, and the second is to make the actual transfer. Suppose the new repository is simply called new-repo
, then the necessary steps are as follows:
git clone git@github.com:<user_or_organization>/new-repo.git
cd new-repo
git remote add old-repo-branch <path-to-movement-example-folder>
path-to-movement-example-folder
would look like ../movement-example
git pull old-repo-branch master
folder-to-move
content in the local new-repo
repository, all you have to do is push. But first you need to do something else.git remote rm old-repo-branch
git push origin master
Done! Now, in your new repository there is only a folder of interest along with the entire history of commits. For example, immediately after the above steps, I saw the following in my new repository:
810 commits!
By the way, you will see only 1 brunch - master
. The procedure transfers only one brunch at a time. If you want to transfer dev
, then you just need to do git checkout dev
and git pull origin dev
after the second step in both steps.
If you need to transfer all 50 (60? 100?) Brunch, then this solution will not be successful because of too much routine work. But I think that for transfer, you just need to save the master and dev branches, because all the feature branches should already be in dev, and you will branch out new branches in the new repository.
Thanks fstep for the hint. You can simply use another guitar utility, the subtree
. All you need is nothing:
git clone <path-to-movement-example>
git remote rm origin
git subtree split --prefix folder-to-move master
master
. This command will calculate the values for a long time, but in the end it will return you something like 253f8a5edd9a4dbbb1d72e5837243e93c92ebfcd
git push git@github.com:<user_or_organization>/new-repo.git 253f8a5edd9a4dbbb1d72e5837243e93c92ebfcd:master --force
--force
flag, because new-repo
will probably have at least a readme file that you (probably) don’t want to pull.Well, if new-repo
is completely fresh and does not even contain the readme file - in other words, it does not contain any files at all, then it is still easier:
git subtree push --prefix folder-to-move git@github.com:<user_or_organization>/new-repo.git master
Source: https://habr.com/ru/post/326132/
All Articles