更新时间:2022-05-19 17:45:38
根据整体规划需求,多个模块的源码共存在一个仓库中。在一些场景执行中,需要考虑执行效率,仅关注部分路径的代码。即仅需获取部分路径的文件。
利用Sparse Checkout模式。官方信息描述如下:
"Sparse checkout" allows to sparsely populate working directory. It uses skip-worktree bit (see git-update-index(1)) to tell Git whether a file on working directory is worth looking at.
"git read-tree" and other merge-based commands ("git merge", "git checkout"…) can help maintaining skip-worktree bitmap and working directory update. $GIT_DIR/info/sparse-checkout is used to define the skip-worktree reference bitmap. When "git read-tree" needs to update working directory, it will reset skip-worktree bit in index based on this file, which uses the same syntax as .gitignore files. If an entry matches a pattern in this file, skip-worktree will be set on that entry. Otherwise, skip-worktree will be unset.
Then it compares the new skip-worktree value with the previous one. If skip-worktree turns from unset to set, it will add the corresponding file back. If it turns from set to unset, that file will be removed.
While $GIT_DIR/info/sparse-checkout is usually used to specify what files are in. You can also specify what files are not in, using negate patterns.
$mkdir code_dir
$cd code_dir
$git init
$git remote add -f origin <url>
$git config core.sparsecheckout true
编辑 .git/info/sparse-checkout
$echo “want_dir/*” >> .git/info/sparse-checkout
$git pull origin master
支持通配符 “*“,如可以写成以下格式:
*docs/
index.*
*.gif
支持排除项 “!”,如只想排除排除项目下的 “docs” 目录,可以按如下格式写:
/*
!/docs/