8. git-submodule 子模塊

Git Submodule 允許一個(gè) git 倉(cāng)庫(kù),作為另一個(gè) git 倉(cāng)庫(kù)的子目錄,并且保持父項(xiàng)目和子項(xiàng)目相互獨(dú)立。

父項(xiàng)目:外層項(xiàng)目
子項(xiàng)目:里面的項(xiàng)目。

常用命令

git submodule 涉及的常用功能有:

  • git clone <repository> –recursive :遞歸的方式克隆整個(gè)項(xiàng)目

  • git submodule add <repository> <path> :添加子模塊

  • git submodule init :初始化子模塊

  • git submodule update :更新子模塊

  • git submodule foreach git pull: 拉取所有子模塊

  • git submodule foreach git checkout -- .: 所有子模塊進(jìn)行 checkout -- . 操作。

NAME
       git-submodule - Initialize, update or inspect submodules

SYNOPSIS
       git submodule [--quiet] add [<options>] [--] <repository> [<path>]
       git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
       git submodule [--quiet] init [--] [<path>...]
       git submodule [--quiet] deinit [-f|--force] (--all|[--] <path>...)
       git submodule [--quiet] update [<options>] [--] [<path>...]
       git submodule [--quiet] summary [<options>] [--] [<path>...]
       git submodule [--quiet] foreach [--recursive] <command>
       git submodule [--quiet] sync [--recursive] [--] [<path>...]
       git submodule [--quiet] absorbgitdirs [--] [<path>...]


DESCRIPTION
       Inspects, updates and manages submodules.

       For more information about submodules, see gitsubmodules(7).

子模塊的添加

git submodule add <url> <path>

其中,url 為子模塊的路徑,path 為該子模塊存儲(chǔ)的目錄路徑。

執(zhí)行成功后,git status 會(huì)看到項(xiàng)目中修改了.gitmodules,并增加了一個(gè)新文件夾(為剛剛添加的路徑)

使用命令 git status 可以看到多了兩個(gè)需要提交的文件,其中 .gitmodules 指定submodule 的主要信息,包括子模塊的路徑和地址信息,moduleA 指定了子模塊的commit id,使用 git diff 可以看到這兩項(xiàng)的內(nèi)容。

然后和往常一樣進(jìn)行 add 和 commit 即可.

需要注意的是,父項(xiàng)目的 git 并不會(huì)記錄 submodule 的文件變動(dòng),它是按照 commit id 指定 submodule 的 git header,所以 .gitmodules 和 moduleA 這兩項(xiàng)是需要提交到父項(xiàng)目的遠(yuǎn)程倉(cāng)庫(kù)的。

克隆帶子模塊的版本庫(kù)

方法一:先 clone 父項(xiàng)目,再初始化 submodule,最后更新 submodule,初始化只需要做一次,之后每次只需要直接 update 就可以了,需要注意 submodule 默認(rèn)是不在任何分支上的,它指向父項(xiàng)目存儲(chǔ)的 submodule commit id。

git clone project.git project2 && cd project2 
git submodule init 
git submodule update 

或者

git clone project.git project2  && cd project2 
git submodule update --init --recursive

方法二:采用遞歸參數(shù) –recursive,需要注意同樣 submodule 默認(rèn)是不在任何分支上的,它指向父項(xiàng)目存儲(chǔ)的 submodule commit id。

git clone project.git project3 –recursive 

子模塊的更新

子模塊的維護(hù)者提交了更新后,使用子模塊的項(xiàng)目必須手動(dòng)更新才能包含最新的提交。
在項(xiàng)目中,進(jìn)入到子模塊目錄下,執(zhí)行 git pull 更新,查看 git log 查看相應(yīng)提交。
完成后返回到項(xiàng)目目錄,可以看到子模塊有待提交的更新,使用 git add,提交即可。

從存儲(chǔ)庫(kù)中刪除所有 Git 緩存的子模塊(Deleting all Git cached submodules from repository)

# deinit all submodules from .gitmodules
git submodule deinit .

# remove all submodules (`git rm`) from .gitmodules
git submodule | cut -c43- | while read -r line; do (git rm "$line"); done

# delete all submodule sections from .git/config (`git config --local --remove-section`) by fetching those from .git/config
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done

# manually remove leftovers
rm .gitmodules
rm -rf .git/modules

I do not know for server synchronisation. It could be done automatically with next commit, or we might need those commands:

git submodule sync
git submodule update --init --recursive --remote

一個(gè)示例

git clone git@gitee.com:mabuo/html.git
# 添加子模塊, 并進(jìn)行一次提交 f'f
git submodule add git@gitee.com:mabuo/html.git lala
git add .
git commit -m 'add submodule'

Git出現(xiàn) fatal: Pathspec 'xxx' is in submodule 解決方案

由于某個(gè)目錄是一個(gè) git 項(xiàng)目.
使用 git add 后只增加了文件夾,但是沒(méi)有文件。手動(dòng) Add 里面單個(gè)文件則報(bào)出錯(cuò)誤信息:
fatal: Pathspec 'xxx' is in submodule

解決方案:
發(fā)現(xiàn) vendor/crazyfd 下面并沒(méi)有 .git 文件
所以使用下面命令:

git rm -rf --cached vendor/crazyfd/yii2-qiniu 
git add vendor/crazyfd/yii2-qiniu/*

參考

Git 出現(xiàn) fatal: Pathspec 'xxx' is in submodule 解決方案
https://blog.csdn.net/JaredFu/article/details/53116578

git submodule的使用_Jacob-wj的博客-CSDN博客_git submod
https://blog.csdn.net/wangjia55/article/details/24400501

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容