본문 바로가기
개발 환경세팅/Git, Github

[Git/Github] Submodule 정리

by minhyeok.lee 2024. 9. 16.
반응형

Git과 Github에서 사용하는 서브모듈이란?

Github 저장소 안에 다른 Github 저장소를 디렉토리로 분리해 넣는 것으로 프로젝트에서 외부 라이브러리를 가져다쓰는 경우에 사용한다.

 


 

1. Submodule 생성

1. 아래 명령어를 통해 Submodule을 생성한다.

git submodule add [서브모듈 repository 주소]

 

2. .gitmodules 파일이 아래와 같은 형식으로 생성된다.

[submodule [서브모듈 이름]]
	path = [서브모듈 위치]
	url = [서브모듈 url]
	branch = [서브모듈 branch]

2. Submodule 업데이트(최신화)

0. Submodule 초기화

git submodule init

 

1. 아래 명령어를 통해 remote 저장소로부터 코드를 local 저장소로 가져온다.

git submodule update --remote [서브모듈이름]

 

만약 서브모듈 이름을 명시하지 않았다면 프로젝트에 포함된 모든 서브모듈에 대해서 update를 실행한다.

 

2. 이 기능은 기본적으로 master를 추적하기에 다른 branch를 추적하고 싶다면 아래와 같이 명령어를 실행한다.

git config -f .gitmodules submodule.[서브모듈이름].branch [추적할브랜치]

 

3. 이때 -f .gitmodules 옵션을 주면 .gitmodule 파일에 branch = master 항목이 추가되어 모든 사용자에게 공유한다.

[submodule [서브모듈 이름]]
	path = [서브모듈 위치]
	url = [서브모듈 url]
	branch = master // 이 부분에 추가

 

만약 -f .gitmodules 옵션을 주지 않으면 이 설정은 해당 사용자에게만 적용된다.

 

 

2-1 git clone을 하며 submodule을 동시에 가져오기

git clone을 하면 submodule까지 가져오지 않는데 아래 명령어를 사용하면 동시에 받을 수 있다.

git clone --recurse-submodules [서브모듈 repository 주소]

새로 clone을 받는 것과 똑같이 수행하기에 .env파일을 다시 받고, nodejs 계열 사용시 npm i와 같은 종속성 설치도 해야한다.

 

2-2 git clone 이후 init, update 한 번에 하는 명령어

git submodule update --init
git submodule update --init [submodule 경로]

 

이 명령으로 init과 update 한 번에 가능하다.


3. Submodule 수정사항 push하는 방법

3-1.첫번째는 submodule의 디렉토리는 다른 remote repository를 바라보고 있어 일반적인 깃허브의 사용법인 add, commit, push 하는 방식을 submodule 작업 디렉토리 안으로 들어가서 수행하면 된다.

 

3-2. git push --recurse-submodules= 사용

git push --recurse-submodules=check
=> push를 하지 않은 서브모듈이 있는지 검사

git push --recurse-submodules=on-demand
=> push를 하지 않은 서브모듈을 자동으로 push처리

git config push.recurseSubmodules on-demand
=> git push의 기본 설정을 추가
반응형

댓글