2022年,繼續(xù)做開源的朋友-續(xù)篇之稀疏檢出
??想了解更多內(nèi)容,請(qǐng)?jiān)L問:??
??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??
見社區(qū)上有些倉庫特別大,下載起來特別花時(shí)間。本篇就專門講述下稀疏檢出,如何只檢出我們關(guān)注的那些文件夾或文件,并整理在《2022年,繼續(xù)做開源的朋友》系列中。
1、 什么是稀疏檢出
如果Git倉庫特別大,每次執(zhí)行Git命令,等待時(shí)間會(huì)特別長(zhǎng)。為解決這些問題,從 1.7.0 開始,Git 引入稀疏檢出( sparse checkout)特性,稀疏檢出機(jī)制允許只檢出指定目錄或者文件,這在大型 Git 倉庫中,將大幅度縮短 Git 執(zhí)行命令的時(shí)間。要想只檢出指定的目錄或文件,需要在 .git/info/sparse-checkout 文件中指定目錄或文件的路徑。
下面將以如何快速檢出倉庫中的https://gitee.com/openharmony/docs部分文件夾為例進(jìn)行演示。
2、為Git配置稀疏檢出
創(chuàng)建一個(gè)目錄docs,再初始化一個(gè) Git 倉庫,以便用稀疏檢出的方式檢出https://gitee.com/openharmony/docs倉庫中的部分文件夾。切換到新創(chuàng)建的目錄,然后使用 git config core.sparseCheckout true 命令開啟 Git 稀疏檢出模式。如下圖所示:
mkdir docs
cd docs
git init
git config core.sparseCheckout true
git config --list
然后編輯該倉庫目錄下的 .git/info/sparse-checkout 文件,指定檢出規(guī)則。.git/info/sparse-checkout 中使用和 .gitignore 相同的匹配模式,例如 非匹配 !/dir2/* 以及 /*.java 等。這里只檢出https://gitee.com/openharmony-sig/online_event倉庫中的/zh-cn/device-dev/kernel 文件夾,并把本地倉庫和遠(yuǎn)程倉庫關(guān)聯(lián)起來。
echo zh-cn/device-dev/kernel >> .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/docs.git
3、檢出代碼
繼續(xù)執(zhí)行命令檢出代碼,檢出效果如圖所示??梢钥闯鰧?shí)際上,只檢出了指定的目錄。
git pull origin master
4、關(guān)閉稀疏檢出
我看看如何檢出全部的文件,執(zhí)行下面的命令,效果如圖:
echo "/*" > .git/info/sparse-checkout
git read-tree -mu HEAD
如果需要徹底關(guān)閉稀疏檢出,配置 Git 的 core.sparseCheckout 為 false 以及移除 .git/info/sparse-checkout 文件即可。
5、git sparse-checkout命令
對(duì)于高版本的git,可以直接使用git sparse-checkout來配置稀疏檢出。git新版本下載地址為https://git-scm.com/downloads。對(duì)git sparse-checkout命令不再一一介紹了,直接體驗(yàn)下即可快速掌握。命令和執(zhí)行效果圖示如下:
mkdir my-docs
cd my-docs/
git init
git sparse-checkout init
git sparse-checkout add /zh-cn/device-dev
git sparse-checkout list
git remote add origin https://gitee.com/openharmony/docs.git
git pull origin master
附錄-參考資料:
https://git-scm.com/docs/git-sparse-checkout。
??想了解更多內(nèi)容,請(qǐng)?jiān)L問:??
??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??