Windows 版 Git 中的文件名太长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22575662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:28:51  来源:igfitidea点击:

Filename too long in Git for Windows

windowsgit

提问by Papa Mufflon

I'm using Git-1.9.0-preview20140217for Windows. As I know, this release should fix the issue with too long filenames. But not for me.

我正在使用Git-1.9.0-preview20140217Windows。据我所知,此版本应该可以解决文件名过长的问题。但不适合我。

Surely I'm doing something wrong: I did git config core.longpaths trueand git add .and then git commit. Everything went well. But when I now do a git status, I get a list of files with Filename too long, for example:

当然,我做错了什么:我没有git config core.longpaths truegit add .git commit。一切顺利。但是当我现在执行 a 时git status,我会得到一个带有 的文件列表Filename too long,例如:

node_modules/grunt-contrib-imagemin/node_modules/pngquant-bin/node_modules/bin-wrapper/node_modules/download/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-handle-source-errors.js: Filename too long

It is quite simple to reproduce for me: just create a Yeomanweb application with the Angular generator ("yo angular") and remove node_modulesfrom the .gitignorefile. Then repeat the aforementioned Git commands.

对我来说,重现非常简单:只需使用 Angular 生成器(“yo angular”)创建一个YeomanWeb 应用程序并node_modules.gitignore文件中删除。然后重复上述 Git 命令。

What am I missing here?

我在这里缺少什么?

回答by sparkym3

You should be able to run the command

您应该能够运行命令

git config --system core.longpaths true

or add it to one of your Git configuration files manually to turn this functionality on, once you are on a supported version of Git. It looks like maybe 1.9.0 and after.

或手动将其添加到您的 Git 配置文件之一以打开此功能,一旦您使用受支持的 Git 版本。看起来可能是 1.9.0 及之后的版本。

回答by iveqy

Git has a limit of 4096 characters for a filename, except on Windows when Git is compiled with msys. It uses an older version of the Windows API and there's a limit of 260 characters for a filename.

Git 的文件名限制为 4096 个字符,但在 Windows 上使用 msys 编译 Git 时除外。它使用旧版本的 Windows API,文件名限制为 260 个字符。

So as far as I understand this, it's a limitation of msys and not of Git. You can read the details here: https://github.com/msysgit/git/pull/110

因此,据我所知,这是 msys 而不是 Git 的限制。您可以在此处阅读详细信息:https: //github.com/msysgit/git/pull/110

You can circumvent this by using another Git client on Windows or set core.longpathsto trueas explained in other answers.

您可以通过在 Windows 上使用另一个 Git 客户端或按照其他答案中的说明设置core.longpaths为来规避此true问题。

git config --system core.longpaths true

Git is build as a combination of scripts and compiled code. With the above change some of the scripts might fail. That's the reason for core.longpaths not to be enabled by default.

Git 是作为脚本和编译代码的组合构建的。通过上述更改,某些脚本可能会失败。这就是默认情况下不启用 core.longpaths 的原因。

The windows documentation at https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-filehas some more information:

https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file上的 Windows 文档有更多信息:

Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.

A registry key allows you to enable or disable the new long path behavior. To enable long path behavior set the registry key at HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)

从 Windows 10 版本 1607 开始,MAX_PATH 限制已从常见的 Win32 文件和目录函数中删除。但是,您必须选择加入新行为。

注册表项允许您启用或禁用新的长路径行为。要启用长路径行为,请将注册表项设置为 HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled(类型:REG_DWORD)

回答by Sagiruddin Mondal

This might help:

这可能有帮助:

git config core.longpaths true

Basic explanation: This answer suggests not to have such setting applied to the global system (to all projects so avoiding --systemor --globaltag) configurations. This command only solves the problem by being specific to the current project.

基本解释:此答案建议不要将此类设置应用于全局系统(因此避免--system--global标记所有项目)配置。此命令仅通过特定于当前项目来解决问题。

回答by Yash

Create .gitconfig and add

创建 .gitconfig 并添加

[core]
longpaths = true

You can create the file in a project location (not sure) and also in the global location. In my case the location is C:\Users\{name}\.

您可以在项目位置(不确定)和全局位置创建文件。在我的情况下,位置是C:\Users\{name}\

回答by Saikat

Steps to follow:

要遵循的步骤:

  1. Run Git Bashas administrator
  2. Run the following command:
  1. 管理员身份运行Git Bash
  2. 运行以下命令:
git config --system core.longpaths true

Note: if step 2 does not work or gives any error, you can also try running this command:

注意:如果第 2 步不起作用或出现任何错误,您也可以尝试运行以下命令:

git config --global core.longpaths true

Read more about git confighere.

阅读更多关于git config这里

回答by Janderson Silva

The better solution is enable the longpath parameter from Git.

更好的解决方案是从 Git 启用 longpath 参数。

git config --system core.longpaths true

But a workaround that works is remove the node_modules folder from Git:

但有效的解决方法是从 Git 中删除 node_modules 文件夹:

$ git rm -r --cached node_modules
$ vi .gitignore

Add node_modules in a new row inside the .gitignore file. After doing this, push your modifications:

在 .gitignore 文件中的新行中添加 node_modules。执行此操作后,推送您的修改:

$ git add .gitignore
$ git commit -m "node_modules removed"
$ git push

回答by Watchmaker

To be entirely sure that it takes effect immediately after the repository is initialized, but before the remote history is fetched or any files checked out, it is safer to use it this way:

为了完全确定它在存储库初始化后立即生效,但在获取远程历史记录或检出任何文件之前,以这种方式使用它更安全:

git clone -c core.longpaths=true <repo-url>

-c key=value

Set a configuration variable in the newly-created repository; this takes effect immediately after the repository is initialized, but before the remote history is fetched or any files checked out. The key is in the same format as expected by git-config1(e.g., core.eol=true). If multiple values are given for the same key, each value will be written to the config file. This makes it safe, for example, to add additional fetch refspecs to the origin remote.

-c 键=值

在新创建的存储库中设置一个配置变量;这在存储库初始化后立即生效,但在获取远程历史记录或检出任何文件之前。密钥的格式与 git-config 1预期的格式相同(例如,core.eol=true)。如果为同一个键指定了多个值,则每个值都将写入配置文件。例如,这使得向源远程添加额外的 fetch refspecs 是安全的。

More info

更多信息

回答by Arpit Aggarwal

Executing git config --system core.longpaths truethrown an error to me:

执行git config --system core.longpaths true向我抛出一个错误:

"error: could not lock config file C:\Program Files (x86)\Git\mingw32/etc/gitconfig: Permission denied"

“错误:无法锁定配置文件 C:\Program Files (x86)\Git\mingw32/etc/gitconfig:权限被拒绝”

Fixed with executing the command at the global level:

修复了在全局级别执行命令:

git config --global core.longpaths true

回答by amalik2205

git config --global core.longpaths true

The above command worked for me. Using '--system' gave me config file not locked error

上面的命令对我有用。使用“--system”给了我配置文件未锁定错误

回答by Julian Veerkamp

You could also try to enable long file paths.

您也可以尝试启用长文件路径。

If you run Windows 10 Home Edition you could change your Registry to enable long paths.

如果您运行 Windows 10 家庭版,则可以更改注册表以启用长路径。

Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystemin regeditand then set LongPathsEnabledto 1.

进入HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSysteminregedit然后设置LongPathsEnabled1

If you have Windows 10 Pro or Enterprise you could also use Local Group Policies.

如果您有 Windows 10 专业版或企业版,您还可以使用本地组策略。

Go to Computer ConfigurationAdministrative TemplatesSystemFilesystemin gpedit.msc, open Enable Win32 long pathsand set it to Enabled.

转到Computer ConfigurationAdministrative TemplatesSystemFilesystemin gpedit.msc,打开Enable Win32 long paths并将其设置为Enabled