Problem
When you perform an import on a Windows machine using Git Bash, you may encounter an ID contained more segments than required error. This occurs when the command attempts to import an Azure resource.
Example command:
$ terraform import azurerm_resource_group.test "/subscriptions/<REDACTED>/resourceGroups/my-test-resource-group"
Example error output:
azurerm_resource_group.test: Importing from ID "C:/Program Files/Git/subscriptions/<REDACTED>/resourceGroups/my-test-resource-group"... ╷ │ Error: │ ID contained more segments than required: │ "C:/Program │ Files/Git/subscriptions/<REDACTED>/resourceGroups/my-test-resource- │ group", map[Program Files:Git] ╵
Cause
This issue occurs because Azure resource IDs begin with a forward slash (/). When you use Git Bash on Windows, it automatically performs a POSIX-to-Windows path conversion, which incorrectly modifies the resource ID by prepending the Git installation path (e.g., C:/Program Files/Git).
Solution
To prevent the path conversion, you must set the MSYS_NO_PATHCONV environment variable to 1 in your shell session before running the import.
-
Set the environment variable.
$ export MSYS_NO_PATHCONV=1
-
Run the
terraform importcommand again. The import should now succeed.$ terraform import azurerm_resource_group.test "/subscriptions/<REDACTED>/resourceGroups/my-test-resource-group"
The output indicates a successful import.
azurerm_resource_group.test: Importing from ID "/subscriptions/<REDACTED>/resourceGroups/my-test-resource-group"... azurerm_resource_group.test: Import prepared! Prepared azurerm_resource_group for import azurerm_resource_group.test: Refreshing state... [id=/subscriptions/<REDACTED>/resourceGroups/my-test-resource-group] Import successful! The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.
Additional Information
This behavior is documented in the Azure CLI guidance for using Git Bash.