Clone Subdirectory of Git Repository

本文介绍了如何使用Git的sparsecheckout功能创建空仓库、添加远程仓库、配置仅克隆特定目录,并展示了从GitHub默认分支(如main)拉取更新的方法。自Git2.25.0以来,sparsecheckout引入了实验性新特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

prerequisites: git >= 1.7.0
original article

(0. Create Empty Repository)

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

Replace <url> with the URL of your remote repository.


1. Do a Sparse Checkout

$ git config core.sparseCheckout true

Next, you need to include the folders and subfolders that you want to clone in .git/info/sparse-checkout file.
Replace /path/to/dir with the folder path.

$ echo "path/to/dir/" >> .git/info/sparse-checkout

2. Pull Main

$ git pull origin main

Notice: main is now the default branch of Github.

Since git 2.25.0 there is a new experimental feature of sparse-checkout. Here are some sample commands using sparse-checkout.

$ git sparse-checkout init                  # git config core.sparseCheckout true
$ git sparse-checkout set "/path/to/folder" # echo "/path/to/folder" >> .git/info/sparse-checkout
$ git sparse-checkout list                  # cat .git/info/sparse-checkout
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值