특정 branch가 역할들을 모두 구현하여
다른 branch와 합치려면 어떻게 해야 할까?
브랜치를 합치는 방법으로는 Merge와 Rebase가 존재한다.
1. Merge (병합)
두 개의 branch를 서로 이어 붙이는데 그 과정에서 합쳐진 커밋이 하나 생기게 된다.
main 브랜치에 초록 브랜치의 내용들을 모두 적용한 다음 커밋을 하는 느낌이다.
그러다 보니 기존 브랜치의 흔적이 남아있게 된다.
merge를 이용해서 브랜치 합치기 (main 브랜치에 develop 브랜치를 합친다.)
1. 합쳐져야 할 브랜치로 이동을 한다.
git switch main
2. 현재 위치한 브랜치로 합칠 브랜치를 merge 한다.
git merge (브랜치 이름)
git merge develop
+ merge도 결과적으로 commit이 되는 것이라 reset으로 다시 되돌릴 수 있다.
+ 합치고 나면 develop 브랜치는 필요가 없어져서 삭제를 해도 된다.
2.Rebase
branch를 그대로 가져와서 대상 branch에 옮겨서 붙여 넣는다.
그러다 보니 기존 브랜치의 흔적이 없어지게 된다.
rebase를 이용해서 브랜치 합치기 (main 브랜치에 develop 브랜치를 합친다.)
1. merge와 반대로 가져올 브랜치로 이동을 한다.
git switch develop
2. 현재 위치한 브랜치를 합칠 대상의 브랜치에 rebase 한다.
git rebase (브랜치 이름)
git rebase main
+ 그러면 아래의 그림처럼 develop은 가장 최신의 커밋에 위치하고 main은 가만히 있다.
그래서 main의 위치를 develop과 같이 최신으로 이동시켜 준다.
git merge develop
그러면 develop은 merge때와 같이 더 이상 필요가 없어져서 삭제해 주면 된다.
참고 및 출처
'Git & GitHub' 카테고리의 다른 글
로컬 저장소와 GitHub 원격 저장소 연결 및 업로드하기 (0) | 2024.01.10 |
---|---|
Vi모드에서 자주 사용하는 명령어 (0) | 2024.01.10 |
branch 생성, 이동, 조회, 수정, 삭제하는 명령어 (0) | 2023.12.18 |
git으로 관리하고 있는 프로젝트 내용을 commit하기 (0) | 2023.12.18 |
이전 버전(commit)으로 되돌리기 (reset, revert) (0) | 2023.12.10 |