Git subtree 를 이용한 Repository 통합

Jongmyung Choi·2023년 11월 15일

개요

프로젝트 진행중 Github 에서 여러개의 레포지토리에서 작업한 내용을 GitLab의 하나의 레포에 통합해야 되는 상황이 생겼다.

Git Subtree를 이용하여 이를 해결하는 방법을 살펴보자.


Subtree 로 직접 추가하여 PUSH

우선 간단하게 다른 Repository 들을 직접 Subtree로 추가하여 Push 하는 방법부터 보자.

이 방법은 Subtree의 Repository 들이 더이상 수정될 일이 없을 때 , 예를 들어 여러개의 Repository에 있는 결과물들을 하나의 Repository 에서 보여주고 싶은 상황에 사용할 수 있다.

1. 결과물을 통합하는 Repository 생성

GitLab 또는 GitHub에서 결과물을 통합하고자 하는 Repository를 생성한다.

2. Repository Clone

1에서 생성한 Repository 를 Clone 한다.

$ git clone <Repository 주소>

3. Subtree 추가

2의 Repository 에 Subtree 로 다른 Repository 들을 추가하여야 한다.

$ git subtree add --prefix=<저장할 이름> <다른 Repository 주소> <branch>

예시) git subtree add --prefix=business-server https://github.com/MZBR-2023/business-service main

4. Push

$ git push -f

Subtree 로 여러 Repository를 관리

이 방법으로는 여러 Repository 들을 Fetch, Pull 등을 통해 동기화 하여 관리하고 PUSH 할 수 있다.

하지만 이 방법에서는 동기화가 잘 안됐을 경우 Conflict가 발생할 수 있으니 주의해야한다.

1,2번 과정은 동일하다.

3. 서브트리로 사용할 원격 저장소 추가

$ git remote add <원격 저장소의 이름> <원격 저장소의 주소>

예시) git remote add business-server https://github.com/MZBR-2023/business-service

4. Subtree 추가

$ git subtree add --prefix=<저장할 이름> <다른 Repository 주소> <branch>

예시) git subtree add --prefix=business https://github.com/MZBR-2023/business-service main

5. PULL, PUSH

// PULL
$ git subtree pull --prefix <폴더 이름> <원격 저장소 이름> <branch>

예시) git subtree pull --prefix business-server business main

// PUSH
$ git subtree push --prefix <폴더 이름> <원격 저장소 이름> <branch>

예시) git subtree push --prefix business-server business main

결과

커밋도 그대로 가지고 가며 여러개의 Repository 들을 통합한 걸 볼 수 있다.

profile
총명한 개발자

0개의 댓글