Windows 下 Git Clone 报错:Filename too long 的解决方案

最近想学习 DDD 架构,顺便克隆了一个有名的 DDD 框架开源项目 刚果商城(CongoMall) ,结果 git clone 时遇到了如下错误:

bash 复制代码
unable to create file 
congomall-test-all/congomall-flow-monitor-agent-test/congomall-flow-monitor-message-provider-test/src/main/java/org/opengoofy/congomall/test/flowmonitor/agent/message/provide/rocketmq/FlowMonitorSpringCloudStreamRocketMQTest.java: 
Filename too long
unable to checkout working tree
warning: Clone succeeded, but checkout failed. 
You can inspect what was checked out with 'git status' and retry with 'git restore --source=HEAD :/'

大概意思是:Windows 文件路径过长 (默认 MAX_PATH = 260),导致 Git 无法创建对应文件。

我网上查了下解决办法,主要有几种,我用的是 方法 C(因为最简单,哈哈😁)。


解决方案

1. 启用 Windows 的长路径支持

Windows 10/11 可以通过组策略或注册表开启长路径。

方法 A:组策略

  1. Win + R 输入 gpedit.msc 打开组策略编辑器。
  2. 路径:计算机配置 -> 管理模板 -> 系统 -> 文件系统 -> 启用 Win32 长路径
  3. 将其设置为 启用
  4. 重启电脑。

方法 B:注册表

如果你的 Windows 没有组策略(家庭版常见),可以用注册表:

powershell 复制代码
reg add HKLM\SYSTEM\CurrentControlSet\Control\FileSystem /v LongPathsEnabled /t REG_DWORD /d 1 /f

然后重启电脑即可。

方法 C:Git 长路径支持(推荐)

直接让 Git 支持长路径:

bash 复制代码
git config --system core.longpaths true

2. 克隆到短路径目录

避免路径过长,比如:

bash 复制代码
cd C:\
git clone https://github.com/xxx/xxx.git c:\repo

3. 使用浅克隆(--depth 1

如果只想快速测试代码,不需要完整历史:

bash 复制代码
git clone --depth 1 https://github.com/xxx/xxx.git

4. Sparse Checkout(只下载指定目录)

只需要某些模块时可以这样:

bash 复制代码
git clone --no-checkout https://github.com/xxx/xxx.git
cd xxx
git sparse-checkout init --cone
git sparse-checkout set some/sub/path
git checkout

5. 如果仓库已部分克隆

可以重新签出:

bash 复制代码
git config core.longpaths true
git restore --source=HEAD :/

总结

最推荐的方式是直接执行:

bash 复制代码
git config --system core.longpaths true

或者把项目克隆到 路径较短 的目录(如 C:\repo)。

相关推荐
天若有情6735 小时前
【C++原创开源】formort.h:一行头文件,实现比JS模板字符串更爽的链式拼接+响应式变量
开发语言·javascript·c++·git·github·开源项目·模版字符串
海盗12346 小时前
在群晖NAS上使用Git Server
git
y小花6 小时前
git常用指令
git
华科大胡子6 小时前
开源项目 Git 贡献全流程拆解
git
极地星光6 小时前
工程中:Git 子模块(submodule) vs 直接依赖(源码/库/包管理器)
git
无限进步_7 小时前
【C++&string】大数相乘算法详解:从字符串加法到乘法实现
java·开发语言·c++·git·算法·github·visual studio
无限进步_8 小时前
【C++】验证回文字符串:高效算法详解与优化
java·开发语言·c++·git·算法·github·visual studio
无限进步_11 小时前
【C++】重载、重写和重定义的区别详解
c语言·开发语言·c++·ide·windows·git·github
历程里程碑11 小时前
1 . Git本地操作:版本控制 跨平台协作 仓库核心
java·开发语言·数据结构·c++·git·gitee·github
华科大胡子12 小时前
Git + 云原生
git