如果git库目录是这样的:
git根目录
project_a/
project_b/
...
并且想为project_a
单独创建一个代码库
# 拉一个新分支
git co -b project_a_repo
# 重构本分支的log,将project_a目录提为根目录并去掉其他文件和log
git filter-branch -f --prune-empty --subdirectory-filter project_a/
# 将新的远端代码库添加到当前工作目录
git remote add project_a_origin git://xxxxxx
# 将新的分支push到新的代码库的master分支
git push -f project_a_origin project_a_repo:master
大功告成,赶快clone
一个新库看看
更复杂的情况
如果你的目录更复杂些,比如:
git根目录
include
project_a
project_b
src
project_a
project_b
想变成:
git根目录
include # 原来的include/project_a
src # 原来的src/project_a
只需要将之前的filter-branch
命令改成:
git filter-branch -f --prune-empty --index-filter step.sh
然后新建一个step.sh
,要有可执行权限,内容:
#!/bin/bash
git read-tree --empty 2>/dev/null # 将目录清空
git read-tree --prefix=include ${GIT_COMMIT}:include/project_a 2>/dev/null # 将include/project_a目录的内容放到include
git read-tree --prefix=src ${GIT_COMMIT}:src/project_a 2>/dev/null # 同上
exit 0
相当于git会重演一遍log,在每个commit重演后都执行一遍step.sh