如何只克隆部分代码?

简介: 如何只克隆部分代码?一、需求根据整体规划需求,多个模块的源码共存在一个仓库中。在一些场景执行中,需要考虑执行效率,仅关注部分路径的代码。即仅需获取部分路径的文件。二、解决方案利用Sparse Checkout模式。

如何只克隆部分代码?

一、需求

根据整体规划需求,多个模块的源码共存在一个仓库中。在一些场景执行中,需要考虑执行效率,仅关注部分路径的代码。即仅需获取部分路径的文件。

二、解决方案

利用Sparse Checkout模式。官方信息描述如下:

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.

三、基本操作过程

1. 准备

$mkdir code_dir
$cd code_dir
$git init
$git remote add -f origin <url>

2. 启用sparsecheckout模式

$git config core.sparsecheckout true

3. 设置排除的路径

编辑 .git/info/sparse-checkout

$echo “want_dir/*” >> .git/info/sparse-checkout

4. 拉取代码

$git pull origin master

四、如何设置sparsecheckout文件

子目录的匹配

  • 如果目录名称前带斜杠,如/docs/,将只匹配项目根目录下的docs目录
  • 如果目录名称前不带斜杠,如docs/,其他目录下如果也有这个名称的目录,如test/docs/也能被匹配。
  • 如果写了多级目录,如docs/05/,则不管前面是否带有斜杠,都只匹配项目根目录下的目录,如test/docs/05/不能被匹配。

通配符 “*“ (星号)

支持通配符 “*“,如可以写成以下格式:

*docs/
index.*
*.gif

排除项 “!” (感叹号)

支持排除项 “!”,如只想排除排除项目下的 “docs” 目录,可以按如下格式写:

/*
!/docs/

五、如何关闭sparsecheckout模式

  1. 将core.sparsecheckout设为false
  2. 修改 .git/info/sparse-checkout 文件,用一个”*“号替代其中的内容
  3. 执行 checkout 或 read-tree 命令

六、注意事项

  1. 如果排除是临时性的,要及时清除相关设置,否则就是一个隐患,即导致环境不可重入。
  2. 仅限 Git 1.7以上版本

参考

目录
相关文章
|
存储 安全 Shell
Git clone 克隆私有项目
Git clone 克隆私有项目
Git clone 克隆私有项目
|
11月前
|
存储
【克隆方法+深浅拷贝】
【克隆方法+深浅拷贝】
61 0
|
开发工具 git
git clone避坑的万能步骤
git clone避坑的万能步骤
1971 1
|
12月前
|
Java 编译器 Apache
每日面试题之介绍一下克隆!
每日面试题之介绍一下克隆!
|
PHP 开发者
对象克隆|学习笔记
快速学习对象克隆
对象克隆|学习笔记
133. 克隆图
133. 克隆图
86 0
133. 克隆图
|
存储 持续交付 开发工具
如何使用Git浅克隆来提高性能
克隆(clone)整个仓库是使用 Git 的标准操作过程。每个克隆通常包括存储库中的所有内容
728 0
|
Shell 开发工具 git
Git存管代码,实例演示
Git 管理我们的代码非常方便呢,当然也可以用来管理我们的文档了。 首先我们来设置每次打开 Git Bash 默认进入我们的工作空间
109 0
Git存管代码,实例演示
|
Java 开发者
对象克隆|学习笔记
快速学习对象克隆
|
开发工具 git
Git命令集之四——克隆命令
Git命令集之四——克隆命令
737 0