This error occurs when adding a Git submodule that conflicts with a cached submodule directory in .git/modules/. Fix it by removing the stale cache, using --force, or providing an alternate --name.
When you run `git submodule add`, Git stores the submodule's repository data in the `.git/modules/<path>` directory of your parent repository. This error indicates that Git found an existing git directory at that location, typically left over from a previously removed submodule. The full error message usually looks like: ``` fatal: A git directory for 'path/to/submodule' is found locally with remote(s): origin [email protected]:example/repo.git If you want to reuse this local git directory instead of cloning again from [email protected]:example/new-repo.git use the '--force' option. If the local git directory is not the correct repo or you are unsure what this means choose another name with the '--name' option. ``` This commonly happens when you: - Removed a submodule but didn't clean up `.git/modules/` - Changed a submodule's remote URL - Moved or renamed a submodule path - Cloned a repository where submodule history diverged Git is being cautious: it won't overwrite existing repository data that might contain commits not pushed elsewhere.
Read the error message carefully. It shows:
1. The path where the conflict exists
2. The existing remote URL in the cached directory
3. The new remote URL you're trying to add
If the remotes match, you can safely use --force. If they differ, you need to decide which repository you actually want.
# View what's in the cached directory
ls .git/modules/path/to/submodule
# Check the cached remote
cat .git/modules/path/to/submodule/config | grep urlIf the cached directory contains the correct repository (same remote URL), use the --force flag to reuse it:
git submodule add --force <repository-url> <path>This tells Git to use the existing .git/modules/<path> directory instead of cloning fresh. This is the fastest solution when the cached data is valid.
Example:
git submodule add --force [email protected]:example/lib.git libs/mylibIf you want a fresh clone or the cached directory has the wrong repository, remove it:
# Remove the cached submodule git directory
rm -rf .git/modules/path/to/submodule
# Also remove the submodule config section
git config --remove-section submodule.path/to/submodule 2>/dev/null || trueNow retry adding the submodule:
git submodule add <repository-url> <path>Example for a submodule at `libs/mylib`:
rm -rf .git/modules/libs/mylib
git config --remove-section submodule.libs/mylib 2>/dev/null || true
git submodule add [email protected]:example/lib.git libs/mylibIf you want to add a different repository at the same path (e.g., replacing a submodule with a fork), use the --name option:
git submodule add --name <unique-name> <repository-url> <path>This stores the submodule data under a different name in .git/modules/<unique-name>, avoiding the conflict.
Example:
# Original was stored as .git/modules/libs/mylib
# Use a new name to avoid conflict
git submodule add --name mylib-fork [email protected]:myfork/lib.git libs/mylibTo fully remove a submodule and avoid this error in the future, use these steps:
Modern Git (2.17+):
# This handles most cleanup automatically
git rm path/to/submodule
git commit -m "Remove submodule"
# Still need to manually clean up .git/modules
rm -rf .git/modules/path/to/submoduleComplete manual removal:
# 1. Deinitialize the submodule
git submodule deinit -f path/to/submodule
# 2. Remove from .git/modules
rm -rf .git/modules/path/to/submodule
# 3. Remove from working tree and index
git rm -f path/to/submodule
# 4. Commit the removal
git commit -m "Remove submodule path/to/submodule"If the issue is a URL mismatch (you updated .gitmodules but the cache has the old URL), sync the configuration:
# Update cached URLs from .gitmodules
git submodule sync
# Reinitialize and update the submodule
git submodule update --init --recursiveThis is often better than deleting the cache, especially if the submodule has the same content and you just changed the remote URL (e.g., switching from HTTPS to SSH).
If you have multiple stale submodule directories, you can clean them all:
# List what's in .git/modules
ls -la .git/modules/
# Compare with current .gitmodules entries
git config --file .gitmodules --get-regexp path
# Remove orphaned directories not in .gitmodules
# (Do this manually after reviewing the lists above)
rm -rf .git/modules/orphaned-submoduleDeinit all submodules and start fresh:
git submodule deinit --all -f
rm -rf .git/modules/*
git submodule update --init --recursive### How Git Stores Submodules
Git stores submodule data in multiple locations:
| Location | Purpose |
|----------|---------|
| .gitmodules | Tracked file with submodule URLs and paths |
| .git/config | Local config with [submodule "name"] sections |
| .git/modules/<path>/ | The actual git repository data |
| <path>/.git | A gitfile pointing to .git/modules/<path> |
The "git directory found locally" error specifically refers to .git/modules/<path>/ existing when Git tries to clone there.
### git submodule absorbgitdirs
If you have submodules with their own .git directories (older Git style), you can move them into the parent's .git/modules/:
git submodule absorbgitdirsThis is mostly for historical repositories or submodules added before Git 1.7.8.
### Nested Submodules
For nested submodules, the path in .git/modules/ includes the full nested path:
.git/modules/
libs/
logging/
utils/
nested-submodule/When cleaning up, make sure you're targeting the correct nested path.
### CI/CD Considerations
In CI pipelines, this error often appears because:
- The runner caches .git/modules/ between builds
- Submodule configuration changed in the meantime
Solutions:
1. Add cache invalidation when .gitmodules changes
2. Run git submodule sync before git submodule update
3. Use fresh clones instead of cached working directories
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git