使用Git Subtree在多个项目中共用同一个子项目

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8427796.html

场景一:已有一个大项目,需要把其中一部分内容独立出来作为共用子项目,被其他项目引用

    一:先分拆

cd 项目A
git subtree split -P 项目A的某个目录 -b 子项目名
cd ../
mkdir tempDir
cd tempDir
git init
git pull ../项目A 子项目名
git remote add 子目录名 子项目仓库地址
git push 子项目 -u 分支
git filter-branch -f --index-filter "git rm -r -f -q --cached --ignore-unmatch 子项目名" --prune-empty HEAD //清理掉原来的commit记录

    二:再在其他项目中引用子项目

cd 项目B
git remote add 子项目源名称 子项目git地址 //添加子项目源
git subtree add --prefix 项目B中用来存放子项目的目录名  子项目源名称 分支

    三:在父项目中提交对子项目的修改

git subtree push --prefix 项目B中用来存放子项目的目录名  子项目源名称 分支//提交代码

    四:在父项目中拉取子项目的更新

git subtree pull --prefix 项目B中用来存放子项目的目录名  子项目源名称 分支//更新代码

场景二:将一个已有项目作为子项目,被其他项目引用

    一:在git上创建一个仓库存放子项目

    二:在其他项目中引用子项目

cd 项目B
git remote add 子项目源名称 子项目git地址 //添加子项目源
git subtree add --prefix 项目B中用来存放子项目的目录名  子项目源名称 分支

     三:在父项目中提交对子项目的修改

git subtree push --prefix 项目B中用来存放子项目的目录名  子项目源名称 分支//提交代码

    四:在父项目中拉取子项目的更新

git subtree pull --prefix 项目B中用来存放子项目的目录名  子项目源名称 分支//更新代码

    参阅文献:

    http://blog.csdn.net/bingshushu/article/details/51244480

    https://segmentfault.com/a/1190000003969060

   

原文地址:https://www.cnblogs.com/ygj0930/p/8427796.html