子模块
介绍
有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目。 也许是第三方库,或者你独立开发的,用于多个父项目的库。 现在问题来了:你想要把它们当做两个独立的项目,同时又想在一个项目中使用另一个。
Git 通过子模块来解决这个问题。子模块允许你将一个 Git 仓库作为另一个 Git 仓库的子目录。 它能让你将另一个仓库克隆到自己的项目中,同时还保持提交的独立。
使用子模块
命令:
git submodule add <url> <文件夹名>
这里的“文件夹名”参数可不指定,默认使用引用的库名
例如:
$ git submodule add git@github.com:huggingface/transformers.git
Cloning into 'D:/AAA-CODE/simulation-OS/transformers'...
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Enumerating objects: 84444, done.
remote: Counting objects: 100% (288/288), done.
remote: Compressing objects: 100% (170/170), done.
remote: Total 84444 (delta 128), reused 192 (delta 91), pack-reused 84156
Receiving objects: 100% (84444/84444), 67.72 MiB | 5.61 MiB/s, done.
Resolving deltas: 100% (60555/60555), done.
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory
子模块在github中的效果
使用子模块后,若将此库推送到github等远程库时,子模块文件夹成为一个超链接,点击该文件会跳转到相应的源库。
如下图中的coco_caption和transformers文件夹,点击之后跳转到对应库的github页面。
官方文档