Flower in my dev/Git
<Git> Subtree & Submodule
꽃선생
2020. 11. 20. 15:18
사전준비
- subtree 와 submodule을 추가할 repo가 있는 상태
### main으로 사용할 저장소 (이하 main)
### subtree로 사용할 저장소 (이하 sub)
### git clone {main_repo}
### cd {main_dir}
$ git clone git@github.com:flower/sub_main.git
$ cd sub_main
$ ls -alh
Subtree
- 등록
### git remote add {sub_repo_name} {sub_repo}
### git remote
### git subtree add -P {sub_dir} {sub_repo_name}
### ls -alh
# ~/sub_main/
$ git remote add git@github.com:flower/subtree_sub.git
$ git remote
$ git subtree add -P test_sub test main
$ ls -alh
- pull
main
### git status
### git add {tracked_file_name}
### git commit -m {comment}
### git push -u origin {main_branch}
# ~/sub_main/
$ git status
$ git add test_sub/flower.py
$ git commit -m "subtree test"
$ git push -u origin main
sub
### git subtree pull -P {sub_dir} {sub_repo_name} {sub_branch}
# ~/sub_main/
$ git pull
$ git subtree pull -P test_sub test main
- push
main
### git status
### git add {tracked_file_name}
### git commit -m {comment}
### git push -u origin {main_branch}
# ~/sub_main/
$ git status
$ git add test_sub/flower.py
$ git commit -m "subtree test"
$ git push -u origin main
sub
###git subtree push -P {sub_dir} {sub_repo_name} {sub_branch}
# ~/sub_main/
$ git subtree push -P test_sub test main
Submodule
- 등록
### cd {main_repo_dir}
### git submodule add {submodule_repo} {submodule_dir}
### submodule_dir 생략 가능
### cat .gitmodules
cd submodule_main
git submodule add git@github.com:dhgwak/submodule_sub.git
cat .gitmodules
### git status
### git commit -m {comment}
### git push
git status
git commit -m "submodule is added"
git push
.gitmodules 파일이 main_repo에 존재한다면 위의 단계는 생략하고
$ git submodule init & git submodule update
- pull/push
- main과 submodule을 각각의 project로 제어
- main은 main_dir에서 submodule은 submodule_dir에서 push/pull
Subtree & Submodule 비교
-
Submoule
- Submodule은 CBD(component-based development)에 적합한 모델이며, 메인 프로젝트는 다른 컴포넌트들에 의존적이다.
- Submodule은 링크다.
- Submoudle은 저장소를 여러개의 작은 저장소로 나눌때 사용한다. 만약 서브 모듈에서 변경을 한다면 서브 모듈 안에서 커밋/푸쉬를 한 후에 메인 저장소(슈퍼 프로젝트)에서 한번 더 커밋/푸쉬를 해야 한다.
-
Subtree
- Subtree는 SBD(system-based development)에 더욱 가까우며, 모든 저장소는 모든것을 포함하고 있으며 각 부분을 수정 가능하다.
- Subtree는 복사다.
- Subtree를 사용하면 다른 저장소를 하나의 저장소로 이력과 함께 통합할수 있다. 통합을 하게 된다면 저장소의 크기는 커지지만 코드와 이력을 재 사용하기에는 더욱 좋습니다. 결국 히스토리를 관리하기에 좋다.