others-how to solve the Filename too long error when using `git clone` in windows:

1. Purpose

In this post, I would demonstrate how to solve the following error when using git clone in windows:

Filename too long

2. The reason and solution

2.1 The reason of this problem

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.So it’s a limitation of msys and not of Git. You can read the details here: https://github.com/msysgit/git/pull/110.

The root cause of the technical limitation of 260 chars lies within the Windows API. Microsoft’s online article Naming Files, Paths, and Namespaces describes the reasons. Because Git was originally written on Linux, there’s no such limitation. Thus the problem occurs when the original Git code is compiled on the Windows platform.

2.2 The solutions

2.2.1 Solution #1

You can solve this problem by using another Git client on Windows or set core.longpaths to true as explained in other answers.

Run the following command (Run as terminal as administrator):

git config --system core.longpaths true

If you encounter this error:

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

You can fix the problem by running this:

git config --global core.longpaths true

The limitation to 260 chars in a path is not specific to MSYS, it’s a general Windows API imitation. This can be worked around by using Unicode paths, but that has other drawbacks, which is why core.longpaths is not enabled by default. Also note that Git for Windows it not compiled against MSYS. Instead, it’s a native Windows application that comes with a stripped-down MSYS environment.

2.2.2 Solution #2

you can Create .gitconfig and add this:

[core]
longpaths = true

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

2.2.3 Solution #3

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.

2.2.4 Solution #4

You could also try to enable long file paths in windows.

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

Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem in regedit and then set LongPathsEnabled to 1.

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

Go to Computer ConfigurationAdministrative TemplatesSystemFilesystem in gpedit.msc, open Enable Win32 long paths and set it to Enabled.

2.3 The future

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)

3. Summary

In this post, I demonstrated how to solve the Filename too long problem when using git clone commands, the key point is to understand why the error happens, you can workaround this by using git commands or just alter windows settings to avoid this problem. That’s it, thanks for your reading.