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

相关推荐
打点计时器2 小时前
Git快速上手教程
git
我才是一卓3 小时前
linux 安装简易 git 服务端并使用
linux·运维·git
IDIOT___IDIOT5 小时前
关于 git 进行版本管理的时候 gitignore 写入忽略规则而不生效的问题
大数据·git·elasticsearch
不想看见4045 小时前
Git 误删急救手册
大数据·git·elasticsearch
偷懒下载原神5 小时前
【linux操作系统】信号
linux·运维·服务器·开发语言·c++·git·后端
IT二叔5 小时前
Git Flow03-发布流程
git
IT二叔5 小时前
Git Flow08-摘樱桃
git
「QT(C++)开发工程师」6 小时前
Git误操作急救手册大纲
git
贺小涛7 小时前
Git代码提交规范和踩坑排水明沟
大数据·git·elasticsearch
IT二叔7 小时前
Git Flow02-开发步骤
git