neiya neiya - 17 days ago 4
Git Question

Move a folder from a repository to another repository

I didn't manage to ask my question properly.
The situation is : I had a repository on github like this :

OldRepository

- Folder 1

- Folder 2

- Folder 3

And I wanted to move one folder from this repository to another one, like this :

OldRepository

- Folder 1

- Folder 2


NewRepository

- Files and folders from Folder 3

I have followed this tutorial : https://lostechies.com/johnteague/2014/04/04/using-git-subtrees-to-split-a-repository/, but I am stuck at this point "Now that lib folder lives in it’s new repository..."

Currently, my repositories on github are exactly the way I want (the way I show you above). But on my computer the three folders are still in the same folder.

What I have is this :

ProjectFolder

- Folder 1

- Folder 2

- Folder 3

And I want to have is this :

ProjectFolder

- Folder 1

- Folder 2


NewProjectFolder

- Content from folder 3

So what I want is to copy the content of Folder 3 from ProjectFolder to NewProjectFolder, and link this NewProjectFolder to NewRepository.
I don't really get what I need to do, I am very new to git.

Answer

As follows, assuming you have only a master branch:

cd old_repo
git subtree split -prefix folder3 -branch temp
git rm -r folder3
git commit -m"Removed folder 3 from my old repo"
git checkout temp
cd ..
cp -r old_repo new_repo
cd new_repo
git branch -D master
cd ../old_repo
git checkout master
git branch -D temp

Maybe you should leave the git rm -r until you're sure you copied the repo properly. Also you may want to rename the branch temp in new_repo to master:

cd new_repo
git checkout -B master
git branch -D temp