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

相关推荐
测试开发技术1 小时前
Git 中如何比较不同版本之间的差异?常用命令有哪些?
git·gitlab·github·面试题
中东大鹅1 小时前
Git仓库使用
git
一朵盆栽2 小时前
Gerrit workflow
git
我是阿呆同学2 小时前
Git--本地仓库的学习
git
测试开发技术13 小时前
Git 中如何查看提交历史?常用命令有哪些?
git·gitlab·github·面试题
Rains042216 小时前
Git Revert 使用指南(基础用法)
git
Tockm19 小时前
Git语义化提交规范及提交模板设置
git
余很多之很多1 天前
借助AI学习开源代码git0.7之三git-init-db
git·学习
明月与玄武1 天前
Jenkins+Docker+Git实现自动化CI/CD
git·docker·jenkins·ci/cd流水线