On Windows, it's not unlikely that you'll encounter issues where you either have a repo that won't clone or files that won't commit. One common scenario that causes this is when doing snapshot testing, particularly with parameterized tests. These tests often generate snapshot files with names that include the test parameters, resulting in very long filenames. One workaround is to move folders into the root of drives or create shorter names, but ultimately, this will cause issues sooner or later.
Fortunately, Windows can handle long files, but it's opt-in for legacy and compatibility reasons.
Enabling Long Paths in Git
To enable long paths in Git, you can set the following system configuration:
git config --system core.longpaths true
Enabling Long Paths in Visual Studio
For Visual Studio, you need to enable long paths by setting a Windows Registry Key. You can do this using PowerShell with the following command:
New-ItemProperty `
-Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
-Name "LongPathsEnabled" `
-Value 1 `
-PropertyType DWORD `
-Force
By following these steps, you can avoid the common pitfalls associated with long file and directory names in your Git repositories on Windows.
Disclaimer
This isn't a silver bullet. While enabling long paths in Windows and Git helps in most scenarios, it's not a complete solution for all situations. Some workloads in Visual Studio can still encounter issues, particularly when paths are passed to other processes like legacy .NET Framework-based tools. If this is your process, you might want to consider using the Pri.LongPath NuGet package, which provides drop-in replacements for System.IO APIs that handle long paths correctly.
This post is licensed under a Creative Commons Attribution 4.0 International License