Loading...
Loading...
Manage Git submodules for including external repositories within a main repository. Use when working with external libraries, shared modules, or managing dependencies as separate Git repositories.
npx skill4agent add supercent-io/skills-template git-submodule.gitmodules# 서브모듈 추가
git submodule add <repository-url> <path>
# 예: libs/lib 경로에 라이브러리 추가
git submodule add https://github.com/example/lib.git libs/lib# 특정 브랜치를 추적하도록 추가
git submodule add -b main https://github.com/example/lib.git libs/libgit add .gitmodules libs/lib
git commit -m "feat: add lib as submodule"# 방법 1: 클론 시 --recursive 옵션
git clone --recursive <repository-url>
# 방법 2: 클론 후 초기화
git clone <repository-url>
cd <repository>
git submodule init
git submodule updategit submodule update --init --recursive# 모든 서브모듈을 원격 최신으로 업데이트
git submodule update --remote
# 특정 서브모듈만 업데이트
git submodule update --remote libs/lib
# 업데이트 + 머지
git submodule update --remote --merge
# 업데이트 + 리베이스
git submodule update --remote --rebase# 메인 저장소가 참조하는 커밋으로 서브모듈 체크아웃
git submodule update# 서브모듈 디렉토리로 이동
cd libs/lib
# 브랜치 체크아웃 (detached HEAD 해제)
git checkout main
# 변경사항 작업
# ... make changes ...
# 서브모듈 내에서 커밋 및 푸시
git add .
git commit -m "feat: update library"
git push origin main# 메인 저장소로 이동
cd ..
# 서브모듈 참조 업데이트
git add libs/lib
git commit -m "chore: update lib submodule reference"
git push# 모든 서브모듈에서 pull
git submodule foreach 'git pull origin main'
# 모든 서브모듈에서 상태 확인
git submodule foreach 'git status'
# 모든 서브모듈에서 브랜치 체크아웃
git submodule foreach 'git checkout main'
# 중첩된 서브모듈에도 명령 실행
git submodule foreach --recursive 'git fetch origin'# 1. 서브모듈 등록 해제
git submodule deinit <path>
# 2. Git에서 제거
git rm <path>
# 3. .git/modules에서 캐시 제거
rm -rf .git/modules/<path>
# 4. 변경사항 커밋
git commit -m "chore: remove submodule"git submodule deinit libs/lib
git rm libs/lib
rm -rf .git/modules/libs/lib
git commit -m "chore: remove lib submodule"
git push# 서브모듈 상태 확인
git submodule status
# 상세 상태 (재귀적)
git submodule status --recursive
# 요약 정보
git submodule summary 44d7d1... libs/lib (v1.0.0) # 정상 (참조 커밋과 일치)
+44d7d1... libs/lib (v1.0.0-1-g...) # 로컬 변경 있음
-44d7d1... libs/lib # 초기화 안 됨# 1. 서브모듈 추가
git submodule add https://github.com/lodash/lodash.git vendor/lodash
# 2. 특정 버전(태그)으로 고정
cd vendor/lodash
git checkout v4.17.21
cd ../..
# 3. 변경사항 커밋
git add .
git commit -m "feat: add lodash v4.17.21 as submodule"
# 4. 푸시
git push origin main# 1. 저장소 클론
git clone https://github.com/myorg/myproject.git
cd myproject
# 2. 서브모듈 초기화 및 업데이트
git submodule update --init --recursive
# 3. 서브모듈 상태 확인
git submodule status
# 4. 서브모듈 브랜치 체크아웃 (개발 시)
git submodule foreach 'git checkout main || git checkout master'# 1. 모든 서브모듈을 원격 최신으로 업데이트
git submodule update --remote --merge
# 2. 변경사항 확인
git diff --submodule
# 3. 변경사항 커밋
git add .
git commit -m "chore: update all submodules to latest"
# 4. 푸시
git push origin main# 프로젝트 A에서
git submodule add https://github.com/myorg/shared-components.git src/shared
# 프로젝트 B에서
git submodule add https://github.com/myorg/shared-components.git src/shared
# 공유 컴포넌트 업데이트 시 (각 프로젝트에서)
git submodule update --remote src/shared
git add src/shared
git commit -m "chore: update shared-components"# GitHub Actions
jobs:
build:
steps:
- uses: actions/checkout@v4
with:
submodules: recursive # 또는 'true'
# GitLab CI
variables:
GIT_SUBMODULE_STRATEGY: recursive
# Jenkins
checkout scm: [
$class: 'SubmoduleOption',
recursiveSubmodules: true
]# 중첩된 모든 서브모듈 초기화
git submodule update --init --recursive
# 중첩된 모든 서브모듈 업데이트
git submodule update --remote --recursive# .gitmodules 파일 수정
git config -f .gitmodules submodule.libs/lib.url https://new-url.git
# 로컬 설정 동기화
git submodule sync
# 서브모듈 업데이트
git submodule update --init --recursive# 1. 서브모듈 내용 백업
cp -r libs/lib libs/lib-backup
# 2. 서브모듈 제거
git submodule deinit libs/lib
git rm libs/lib
rm -rf .git/modules/libs/lib
# 3. 백업 복원 (.git 제외)
rm -rf libs/lib-backup/.git
mv libs/lib-backup libs/lib
# 4. 일반 파일로 추가
git add libs/lib
git commit -m "chore: convert submodule to regular directory"# 얕은 클론으로 서브모듈 추가
git submodule add --depth 1 https://github.com/large/repo.git libs/large
# 기존 서브모듈을 얕은 클론으로 업데이트
git submodule update --init --depth 1--recursive--depthgit submodule statusgit submodule update --init.gitmodules.git/modules# 강제 초기화
git submodule update --init --force# 서브모듈 상태 확인
git submodule status
# 충돌 해결 후 원하는 커밋으로 체크아웃
cd libs/lib
git checkout <desired-commit>
cd ..
git add libs/lib
git commit -m "fix: resolve submodule conflict"# SSH URL 사용
git config -f .gitmodules submodule.libs/lib.url git@github.com:org/private-lib.git
git submodule sync
git submodule update --init# 서브모듈 내 변경사항 확인
cd libs/lib
git status
git diff
# 변경사항 버리기
git checkout .
git clean -fd
# 또는 커밋하기
git add .
git commit -m "fix: resolve changes"
git push# diff에서 서브모듈 변경 표시
git config --global diff.submodule log
# status에서 서브모듈 요약 표시
git config --global status.submoduleSummary true
# push 시 서브모듈 변경 확인
git config --global push.recurseSubmodules check
# fetch 시 서브모듈도 함께 fetch
git config --global fetch.recurseSubmodules on-demand[submodule "libs/lib"]
path = libs/lib
url = https://github.com/example/lib.git
branch = main
[submodule "vendor/tool"]
path = vendor/tool
url = git@github.com:example/tool.git
shallow = true