홈/엔지니어/Git / CI·CD/Git 서브모듈 — 추가·업데이트·삭제…
Git / CI·CD중급UbuntuDebianmacOSgitsubmodule서브모듈

Git 서브모듈 — 추가·업데이트·삭제

Git 서브모듈로 외부 레포지토리를 프로젝트에 포함하고, 초기화·업데이트·삭제하는 방법과 흔한 실수 해결법을 설명합니다.

목차
  1. 서브모듈이란?
  2. 서브모듈 추가
  3. 서브모듈 포함 레포 클론
  4. 서브모듈 업데이트
  5. 서브모듈 삭제
  6. 상태 확인
  7. 자주 발생하는 문제
  8. GitHub Actions에서 서브모듈

서브모듈이란?

하나의 Git 레포지토리 안에 다른 Git 레포지토리를 포함하는 방법입니다. 공통 라이브러리, 서드파티 코드, 공유 설정을 여러 프로젝트에서 재사용할 때 사용합니다.


서브모듈 추가

Bash
# 서브모듈 추가
git submodule add https://github.com/org/shared-lib.git libs/shared

# 특정 브랜치 추적
git submodule add -b main https://github.com/org/shared-lib.git libs/shared

# 추가 후 생성되는 파일
# .gitmodules — 서브모듈 설정 파일
cat .gitmodules
# [submodule "libs/shared"]
#     path = libs/shared
#     url = https://github.com/org/shared-lib.git
#     branch = main

# 커밋
git add .gitmodules libs/shared
git commit -m "feat: add shared-lib submodule"

서브모듈 포함 레포 클론

Bash
# 방법 1: 클론 시 한 번에
git clone --recurse-submodules https://github.com/org/myproject.git

# 방법 2: 이미 클론된 경우
git submodule init
git submodule update

# 한 줄로
git submodule update --init --recursive

서브모듈 업데이트

Bash
# 특정 서브모듈을 최신으로
cd libs/shared
git pull origin main
cd ../..
git add libs/shared
git commit -m "chore: update shared-lib to latest"

# 모든 서브모듈 한 번에 최신으로
git submodule update --remote --merge

# 병렬 업데이트 (서브모듈 많을 때)
git submodule update --remote --jobs 4

서브모듈 삭제

Bash
# 1. .gitmodules에서 항목 제거
git config -f .gitmodules --remove-section submodule.libs/shared

# 2. .git/config에서 제거
git config --remove-section submodule.libs/shared

# 3. 스테이징에서 제거
git rm --cached libs/shared

# 4. 파일 삭제
rm -rf libs/shared

# 5. .git/modules 정리
rm -rf .git/modules/libs/shared

# 6. 커밋
git add .gitmodules
git commit -m "chore: remove shared-lib submodule"

상태 확인

Bash
# 서브모듈 상태
git submodule status
# + abc1234 libs/shared (v1.2.3)
# ^ = 앞서 있음, - = 초기화 안 됨, + = 변경됨

# 서브모듈 포함 전체 상태
git status --recurse-submodules

# 서브모듈 요약
git submodule summary

자주 발생하는 문제

Bash
# 문제: 서브모듈이 빈 디렉터리
git submodule update --init --recursive

# 문제: detached HEAD 상태
cd libs/shared
git checkout main
git pull

# 문제: URL 변경 후 업데이트 안 됨
git submodule sync
git submodule update --init

# 문제: 서브모듈 변경사항을 부모 레포가 인식 못 함
cd ..
git add libs/shared    # 포인터(커밋 해시)를 업데이트
git commit -m "chore: update submodule pointer"

GitHub Actions에서 서브모듈

YAML
- uses: actions/checkout@v4
  with:
    submodules: recursive      # 서브모듈 포함 체크아웃
    token: ${{ secrets.PAT }}  # private 서브모듈은 PAT 필요
#git#submodule#서브모듈#모노레포#의존성
편집 안내 · Editorial Note

AI 도구가 초안 작성에 사용될 수 있습니다. 명령어의 실행 여부와 검증 환경은 각 가이드에 기재된 근거를 확인하세요. 적용 전 사용 중인 버전의 공식 문서를 확인하고, 오류는 이메일로 알려주세요. 이메일로 제보

주제 관련 공식 문서(자동 연결 · 문장별 근거 아님)Git 공식 문서 ↗

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.