Interview Prep/Git & GitHub/Submodules, LFS and Hooks

Re-adding a removed submodule fails: `A git directory for 'libs/common' is found locally with remote(s): origin https://github.com/org/common.git`.

AdvancedTroubleshootingSubmodules, LFS and Hooks
Answer

Likely causes → Removing a submodule leaves its repository in .git/modules/libs/common. Git refuses to silently reuse it, in case it belongs to a different repo.

Diagnose →

ls .git/modules/libs/
git config -f .git/modules/libs/common/config remote.origin.url

Fix → If it is the same repo, reuse it: git submodule add --force https://github.com/org/common.git libs/common. If it is a different repo, delete the stale directory first: rm -rf .git/modules/libs/common, or pick another name with --name.

Prevent → Remove submodules completely:

git submodule deinit -f libs/common
git rm -f libs/common
rm -rf .git/modules/libs/common
All Git & GitHub questions Roadmap: Git & GitHub

More on Submodules, LFS and Hooks