자바스크립트를 활성화 해주세요

d061 잘못된 submodule path가 .git에 남아있을 때, 삭제하는 방법

 ·  ☕ 2 min read

project를 deploy할 때라던지, 일시적으로 추가했던 submodule이 불필요해져서 삭제하는 경우가 있습니다. 이렇게요.

rm -rf <submodule path>

submodule 디렉토리만 지우게 되면, 제일 상위 .git에는 submodule정보가 그대로 남아 있어서 git 커맨드를 사용하면 이런 에러메시지가 나오는 때가 있습니다.

Failing build: Failed to prepare repo
Error checking out submodules: fatal: No url found for submodule path 'path-to-target' in .gitmodules
Failing build: Failed to prepare repo
failed during stage 'preparing repo': Error checking out submodules: fatal: No url found for submodule path 'path-to-target' in .gitmodules

아, git rm을 안했구나. 이렇게 하면 될까요?

git rm --force <submodule path>
rm -rf <submodule path>

그래도 $GIT_DIR/modules/<submodule name>/ 이 남아 있습니다. 수동으로 이 디렉토리를 지운다면 해결되는 것 같습니다만.

해결책

제일 상위의 .git 에 이미 삭제한 submodule 정보도 지워줘야죠

git rm -r --cached --ignore-unmatch path-to-target/

그리고 commit해서 저장해 줍니다.

git commit

원래라면

원래라면 그냥 submodule 디렉토리를 삭제하지 말고, 이렇게 하라고 하는데, 조금 깁니다.

git submodule add https://github.com/<me>/project path-to-target
git submodule deinit path-to-target
git rm path-to-target
git commit-m "Removed submodule "
rm -rf .git/modules/<path_to_submodule>
# git config -f .gitmodules --remove-section submodule.path-to-target 2> /dev/null # 혹시 git v1.8.5 이전이라면

귀찮아서 까먹고, 그냥 지웠다가 에러나서 다시 git rm -r --cached 하곤 합니다.

deinitrm을 한 번에 하는 방법은 없을까요? 스크립팅 하는 방법이외에 아직 없나봅니다. 왜냐하면 git은 directory를 직접 건들이지 않는 정책인 것 같거든요. git document는 다음과 같이 설명하고 있습니다.

Git directory is kept around as it to make it possible to checkout past commits without requiring fetching from another repository.

d061_git_submodule_document.png

Ref

공유하기

tkim
글쓴이
tkim
Software Engineer