[Git] CLI로 3-way 병합하기

2022. 4. 22. 15:18Git

1. 긴급한 버그 처리 시나리오

1. (옵션) 오류가 없는 버전(주로 Tag가 있는 커밋)으로 롤백
2. [main] 브랜치로부터 [hotfix] 브랜치 생성
3. 빠르게 소스 코드 수정 / 테스트 완료
4. [main] 브랜치로 병합(Fast-forward) 및 배포
5. 개발 중인 브랜치에도 병합 (충돌 발생 가능성이 높음)

 

새로운 브랜치 및 커밋 생성

ch06_git-playground             $ git checkout main # main 브랜치로 이동

(main) ch06_git-playground      $ git checkout -b feature1 # feature1 브랜치 생성 및 이동

(feature1) ch06_git-playground  $ echo "기능 1 추가" >> file1.txt # 파일 내용 수정

(feature1) ch06_git-playground  $ git add file1.txt # 스테이징

(feature1) ch06_git-playground  $ git commit -m "update file1.txt" # 스테이징

(feature1) ch06_git-playground  $ git log --oneline --graph --all -n2 # 로그 확인

 

hotfix 브랜치 생성, 커밋, main에 병합

(feature1) ch06_git-playground $ git checkout -b hotfix main # main으로부터 hotfix 브랜치 생성 및 이동

(hotfix) ch06_git-playground   $ git log --oneline --all -n2 # 2개의 커밋 로그만 보기

(hotfix) ch06_git-playground   $ echo "some hot fix" >> file1.txt

(hotfix) ch06_git-playground   $ git add file1.txt

(hotfix) ch06_git-playground   $ git commit -m "fix some bug"

(hotfix) ch06_git-playground   $ git log --oneline -n1

(hotfix) ch06_git-playground   $ git checkout main

(main)   ch06_git-playground   $ git merge hotfix	# 빨리 감기 병합

(main)   ch06_git-playground   $ git push origin main # 원격 저장소로 push

[main] 브랜치는 [hotfix] 브랜치와 병합하였으므로 버그 수정은 끝났습니다. 하지만 현재 개발중인 [feature1] 브랜치에도 이를 반영해야 합니다. 그러나 [feature1] 브랜치와 [main] 브랜치는 다음과 같이 서로 다른 분기로 진행되고 있습니다.

[main] 브랜치의 file1.txt 내용
hello git
second
third - my branch
fourth - my branch
some hot fix

[feature1] 브랜치의 file1.txt 내용
hello git
second
third - my branch
fourth - my branch
기능 1 추가 # <- 충돌 발생 지점

 

병합 및 충돌 해결하기 1

(main) ch06_git-playground     $ git checkout feature1 # feature1 브랜치로 이동

(feature1) ch06_git-playground $ git log --oneline --all -n3 # 로그 확인

(feature1) ch06_git-playground $ git merge main # main 브랜치와 병합 시도, 충돌 발생

(feature1|MERGING) ch06_git-playground $ git status # 실패 원인 파악하기

  • git merge --abort : merge 취소

 

충돌이 발생한 file1.txt 수정

file1.txt before

file1.txt after

 

병합 및 충돌 해결하기2

(feature1|MERGING) ch06_git-playground $ cat file1.txt # 최종 변경 내용 확인

(feature1|MERGING) ch06_git-playground $ git add file1.txt # 스테이징

(feature1|MERGING) ch06_git-playground $ git status

(feature1|MERGING) ch06_git-playground $ git commit -m "fix some bug"

(feature1|MERGING) ch06_git-playground $ git log --oneline --all --graph -n4 # 로그 확인

 

References

source code : https://github.com/yonghwankim-dev/git_study
팀 개발을 위한 Git Github 시작하기, 정호영 진유림 저

'Git' 카테고리의 다른 글

[Git] git add 명령의 동작 원리  (0) 2022.04.22
[Git] CLI로 rebase 수행  (0) 2022.04.22
[Git] CLI로 checkout 하기  (0) 2022.04.22
[Git] CLI로 브랜치 생성하기  (0) 2022.04.22
[Git] 원격저장소 관련 CLI 명령어  (0) 2022.04.22