文章目录
  1. 1. 拆分子目录
  2. 2. 提取子目录文件
  3. 3. 添加子库远端并提交
  4. 4. 添加子库并清理子目录

本文用来讲解如何拆分 Git repo 子目录为子库,以 git-submodule 为例:

::bash
$ ls 
github.md  README.md  submd5

下面将子目录 submd5 拆成 git-submodule 的子库:

拆分子目录

请使用 git subtree 拆分,操作如下:

::bash
$ git subtree split -P submd5 -b submd5
Created branch 'submd5'
b33806be7badec2da128fcd54d69f4d44beb347f

提取子目录文件

使用 git pull 提取子目录 submd5 中文件及提交记录:

::bash
$ cd ..
$ git init
初始化空的 Git 版本库于 /home/mutse/workspace/gits/submd5/.git/

$ git pull ../git-submodule submd5
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 ../git-submodule
 branch            submd5     -> FETCH_HEAD

添加子库远端并提交

::bash
$ git remote add origin git@github.com:mutse/submd5.git

$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 232 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:mutse/submd5.git
 [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

添加子库并清理子目录

::bash
$ cd ../git-submodule
$ git rm -r submd5

$ git submodule add git@github.com:mutse/submd5.git submd5
正克隆到 'submd5'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
接收对象中: 100% (3/3), done.
检查连接... 完成。

$ git commit -m "add submodule"
[master 33ac4ff] add submodule
 3 files changed, 4 insertions(+), 1 deletion(-)
create mode 100644 .gitmodules
create mode 160000 submd5
delete mode 100644 submd5/sub.md

$ git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 402 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:mutse/git-submodule.git
 d90cc8a..33ac4ff  master -> master
文章目录
  1. 1. 拆分子目录
  2. 2. 提取子目录文件
  3. 3. 添加子库远端并提交
  4. 4. 添加子库并清理子目录