ETC

[Github 도구] submodule

jungeun919 2024. 6. 28. 15:08

깃허브에 알고리즘 풀이 관련 레포지토리가 여러 개로 나누어져 있어서 이를 하나로 통합해서 관리하고, 동시에 기존 커밋도 그대로 유지할 수 있는 방법을 찾아보았다. 그래서 github 도구 중 하나인 submodule을 알게 됐다.

submodule

  • git 프로젝트 안에 다른 git 저장소를 하위 디렉토리에 포함시켜 관리하는 도구
  • 상위 저장소: 슈퍼 프로젝트 / 하위 저장소: 서브모듈
  • 각 저장소의 커밋을 독립적으로 관리
  • 서브모듈 저장소가 private일 경우, 해당 모듈을 클릭했을 때 접근할 수 없음
  • 서브모듈 저장소가 업데이트 돼도 슈퍼 프로젝트에 자동으로 반영되지 않고 직접 수행해야 함
  • 이를 통해 복잡한 프로젝트를 여러 저장소로 분리하여 개발, 관리, 배포를 용이하게 함

 

command

서브모듈 등록(추가)

$ git submodule add [git-repository-url] [path]
$ git submodule add https://github.com/jungeun919/sub-repo.git lib

git-repository-url: 추가하려는 submodule의 git 저장소 url
path: submodule을 포함시킬 하위 디렉토리 경로


추가 후 .gitmodules 파일이 자동으로 생성
이 파일은 서브 디렉토리와 하위 프로젝트의 URL의 매핑 정보를 담음
.gitignore 파일과 마찬가지로 버전관리 대상
추가한 서브모듈은 staging 영역으로 자동으로 올라감

$ cat .gitmodules
[submodule "lib"]
	path = lib
	url = https://github.com/jungeun919/sub-repo.git


commit과 push 후, repository를 확인하면 다음과 같이 서브모듈_이름 @commit_hash 값이 표시된다.

 

서브모듈 변경사항을 슈퍼 프로젝트에 반영

1. 서브모듈 디렉토리에서 새로운 파일을 생성하고 커밋한다.

2. 슈퍼 프로젝트 디렉토리로 이동 후, git pull을 한다.

$ git pull
Your configuration specifies to merge with the ref 'refs/heads/main'
from the remote, but no such ref was fetched.

 

변경된 내용이 없다고 pull을 하지 않는다.

3. lib는 슈퍼 프로젝트 저장소와 독립된 저장소이기 때문에 lib 디렉토리로 이동 후, 다시 git pull을 진행한다.

$  git pull
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0
Unpacking objects: 100% (4/4), 834 bytes | 208.00 KiB/s, done.
From https://github.com/jungeun919/Baekjoon
   ef59c7e..fc4c264  main       -> origin/main
Updating ef59c7e..fc4c264
Fast-forward
 Breadth_First_Search/2636.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 Breadth_First_Search/2636.py

 

간단하게 서브모듈 최신 커밋 반영

슈퍼 프로젝트 디렉토리에서 아래 명령어를 입력한다.
서브모듈에서 변경된 부분을 가져온다.

$ git submodule update --remote [submodule-path]
$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Baekjoon (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

 

 

git status 명령어를 입력하면 서브모듈에 새로운 커밋이 생성되었다.
git add / commit / push 를 수행하여 원격 저장소에 올려서 업데이트 된 것을 확인할 수 있다.


commit hash 값이 ef59c7e에서 fc4c264로 바뀐 것을 확인할 수 있다.

서브모듈 삭제

$ git rm -f [submodule-path] # staging 영역에 올라감
$ git commit -m "message"
$ git push

 

Ref

'ETC' 카테고리의 다른 글

Mac에서 Spotlight 검색 안됨 해결  (0) 2024.06.28
MySQL 문법 정리  (0) 2024.06.28
기본 터미널 명령어 요약  (0) 2024.06.26
터미널 사용법  (0) 2024.06.26