Submodule은git저장소 안에 다른git저장소를 분리해서 넣는 것이다.
- A 프로젝트에서 쓰던 코드를 B 프로젝트에서도 사용해야 할 경우에 사용
- 저장소라 하위에
.git파일이 있음을 알 수 있다.
$ git submodule add [서브모듈 url]
$ git submodule add https:github.com/username/subrepo.git
서브모듈이 추가되면, 서브모듈 이외에
.gitmodules라는 파일도 새롭게 생성되었다.
.gitmodules파일은 서브 저장소와 하위 프로젝트 url의 매핑 정보를 담는다.
모든
submodule을clone해주는 옵션이다.
$ git clone --recursive {주소}
$ git clone --recursive https://github.com/username/repo.git
일반적으로
clone한 후submodule을 업데이트하는 방법
main project에 연결되어 있는subrepo의 정보를 가져와서 업데이트 한다.
$ git submodule update --init --recursive
특정
submodule을 지정해서 업데이트 할 수도 있다.
- 모든
submodule을 다 가져와서 업데이트할 필요가 없기 때문이다.
$ git submodule update --init --recursive ./newsubrepo
--remote는remote repo의 최신 정보를 가져와서update하는 것이다.
remote repo에 새로운commit이 있는 경우local main project에 새로운sub project link가 연결되기 때문에main project가update된다. 따라서main project에 변화가 생길 수 있으며,main project를 새로commit해야 한다
$ git submodule update --remote {remote repo} --merge