Handling Long File Paths with Git on Windows
Git, while a powerful version control system, historically encountered issues with very long file paths, particularly on Windows. This is due to limitations in the Windows API, and how Git interacts with it. This tutorial will guide you through understanding the limitations and configuring Git to handle long paths effectively.
Understanding the Limitations
Traditionally, the Windows API imposed a MAX_PATH
limit of 260 characters for file paths. This means any file or directory path exceeding this length could cause issues with various applications, including Git. While newer versions of Windows (Windows 10 version 1607 and later) have removed this limitation, you need to explicitly enable long path support.
Git itself has a general filename limit of approximately 4096 characters, but on older Windows systems utilizing the msys framework, it was often constrained by the 260-character Windows limit.
Enabling Long Path Support in Windows
Before configuring Git, ensure long path support is enabled in Windows itself. This involves modifying a registry setting:
-
Open PowerShell as Administrator: Right-click the Windows Start button and select "Windows PowerShell (Admin)" or "Terminal (Admin)".
-
Check Current Setting: Run the following command to check the current status of
LongPathsEnabled
:Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled
-
Enable Long Paths: If the output shows
LongPathsEnabled : 0
, enable long paths with the following command:Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -Type DWord -Value 1
Important: Changes to this registry setting require a system restart to take effect.
Configuring Git for Long Paths
Once long path support is enabled in Windows, you need to configure Git to utilize it. There are several ways to do this, each with its scope:
-
System-Wide Configuration: This configures Git for all users and repositories on the system. This requires administrator privileges.
git config --system core.longpaths true
-
Global Configuration: This configures Git for the current user, affecting all repositories they work with.
git config --global core.longpaths true
-
Repository-Specific Configuration: This configures Git only for the current repository.
git config core.longpaths true
Choose the configuration scope that best suits your needs. System-wide is useful for shared environments, while global or repository-specific configurations offer more granular control.
Important Considerations:
- Restart Git Bash/Terminal: After modifying the configuration, close and reopen your Git Bash terminal or command prompt for the changes to take effect.
- GUI Clients: Some Git GUI clients might not automatically read the system-wide configuration. You may need to configure the client separately or use a global configuration.
- Compatibility: While enabling long paths solves many issues, some older tools or applications might not fully support them.
Verification
To verify that long path support is enabled:
- Create a directory and file with a long path (exceeding 260 characters).
- Initialize a Git repository in that directory.
- Add and commit the file.
If Git successfully handles the file without errors, long path support is configured correctly.