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)。

相关推荐
xuxie9913 小时前
GIT版本管理
git
茶栀(*´I`*)13 小时前
Git从零开始操作清单
git
李永奉16 小时前
Git和GitHub-Git 管理的版本数据到底保存在哪个文件夹里?
git·github
Mortalbreeze16 小时前
Git 基础篇(二):本地仓库管理与常用操作
git
武昌库里写JAVA3 天前
SpringBoot+Vue 项目介绍
java·javascript·git·sql·ai
Hvitur5 天前
Git - 多分支合并
git·github
考虑考虑5 天前
git刷新远程分支列表
git·后端·gitlab
AI大模型-小华6 天前
Codex 自动测试怎么用?从生成测试用例到修复失败用例
git·测试用例·ai编程·软件开发·codex·chatgpt pro·codex cli
七分辣度6 天前
GitHub/Git 常用命令及使用场景
git·github
慧都小项7 天前
MyEclipse Inline Diff 实用指南:逐处核对 AI 修改再提交
git·ai编程·myeclipse·java 代码审查·ai 代码修改