发现这篇精彩的文章原创参考很容易理解。记录在这里,以防无法访问。
1.准备当前存储库
$ cd path/to/repository
$ git subtree split -P my-folder -b my-folder
Created branch 'my-folder'
aecbdc3c8fe2932529658f5ed40d95c135352eff
文件夹的名称必须是相对路径,从存储库的根目录开始。
2.创建新存储库
$ cd my-folder
$ git init
Initialized empty Git repository in /Users/adamwest/Projects/learngit/shop/my-folder/.git/
$ git add .
$ git commit -m "initial commit"
[master (root-commit) 192c10b] initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
这里我们只需要cd到新文件夹,初始化新存储库,并提交任何内容。
3.添加新的远程存储库并推送
$ git remote add origin git@github.com:robertlyall/my-folder.git
$ git push origin -u master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 199 bytes | 199.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:robertlyall/my-folder.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
我们在这里添加远离GitHub的新存储库,然后推送我们的第一次提交。
4.从主存储库中删除文件夹并推送
$ cd ../
$ git rm -rf my-folder
rm 'my-folder/file'
$ git commit -m "Remove old folder"
[master 56aedbe] remove old folder
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 my-folder/file
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 217 bytes | 217.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:robertlyall/shop.git
74dd8b3..56aedbe master -> master
最后,我们cd回到rooot目录,从主存储库中删除文件夹,然后提交并推送更改。现在,我们的主存储库中有一个文件夹,但它链接到一个完全独立的存储库,可以在多个项目中重用。