Git

Git 실습 :: git merge로 브랜치 병합 서비스 이용

gamjadori 2024. 5. 7. 10:20
728x90

<Git 실습 :: git merge로 브랜치 병합 서비스 이용>

1. main 브랜치에서 add-coach 브랜치 내용 병합

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (add-coach)
$ git switch main
Switched to branch 'main'

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
  • 병합 과정 수행
  • 충돌이 발생하지 않아 자동 병합 진행
  • main 브랜치에는 add-coach 브랜치의 변경 사항이 병합
  • 새로운 파일 jaguars.yaml과 puma.yaml이 추가
  • leopards.yaml, panthers.yaml, tigers.yaml 파일에서는 충돌이 없이 자동 병합
admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git merge add-coach
Auto-merging leopards.yaml
Auto-merging panthers.yaml
Auto-merging tigers.yaml  
Merge made by the 'ort' strategy.
 jaguars.yaml  | 10 ++++++++++
 leopards.yaml |  2 +-
 panthers.yaml |  2 +-
 puma.yaml     | 10 ++++++++++
 tigers.yaml   |  2 +-
 5 files changed, 23 insertions(+), 3 deletions(-)
 create mode 100644 jaguars.yaml
 create mode 100644 puma.yaml

 

  • 병합 후에는 병합된 브랜치 삭제 권장
admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git branch -d add-coach
Deleted branch add-coach (was fc89b9a).

 

2. 새로운 브랜치 생성 및 rebase로 작업 상황 공유

  • git rebase: 현재 브랜치의 커밋을 다른 브랜치의 최신 커밋 위에 재배치
  • 다른 브랜치에서 새로운 변경 사항이 있을 때, 현재 작업 중인 브랜치에도 최신 변경 사항 적용으로 동기화 작업 수행하고 커밋 히스토리 정리하여 병합 시 충돌 최소화
admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git switch new-teams
Switched to branch 'new-teams'

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (new-teams)
$ git rebase main
Successfully rebased and updated refs/heads/new-teams.

 

  • 브랜치 간 충돌 상황을 만들어 병합 충돌 실습 진행

1. 브랜치 생성 (conflict-1 / conflict-2)

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git branch conflict-1

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git branch conflict-2

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git branch
  conflict-1
  conflict-2
* main

 

  • 서로 다른 내용으로 파일 수정 작업 진행

2. main 브랜치에서 파일 수정

<leopards.yaml>

team: Leopards

manager: Nicholas

coach: Oscar
members:
- Linda
- William
- David
- Olivia

<tiger.yaml>

team: Tigers

manager: Kenneth

coach: Grace
members:
- Linda
- William
- David

<panthers.yaml>

team: Panthers

manager: Shirley

coach: Teddy
members:
- Viloet
- Stella
- Anthony
- Freddie

<커밋>

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git commit -am "Edit Tigers, Leopards, Panthers"
[main 2a3f4c9] Edit Tigers, Leopards, Panthers   
 3 files changed, 3 insertions(+), 3 deletions(-)

 

3. conflict-1 브랜치에서 파일 수정

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git switch conflict-1
Switched to branch 'conflict-1'

<tiger.yaml>

team: Tigers

manager: Deborah

coach: Grace
members:
- Linda
- William
- David

<커밋>

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-1)
$ git commit -am "Edit Tigers"
[conflict-1 7f1f0e4] Edit Tigers
 1 file changed, 1 insertion(+), 1 deletion(-)

 

4. conflict-2 브랜치에서 파일 수정

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-1)
$ git switch conflict-2
Switched to branch 'conflict-2'

<leopards.yaml>

team: Leopards

manager: Luke

coach: Melissa
members:
- Linda
- William
- David
- Olivia

<커밋>

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-2)
$ git commit -am "Edit Leopards"
[conflict-2 b4f8f22] Edit Leopards
 1 file changed, 1 insertion(+), 1 deletion(-)

 

5. main 브랜치에서 conflict-1과 병합 진행

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-1)
$ git switch main
Switched to branch 'main'

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git merge conflict-1
Auto-merging tigers.yaml
CONFLICT (content): Merge conflict in tigers.yaml
Automatic merge failed; fix conflicts and then commit the result.
  • 병합 충돌 발생 확인

 

  • git status에서도 "tigers.yaml" 파일 충돌 상태 표시
admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main|MERGING)
$ git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")        
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   tigers.yaml

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

<병합 작업 중단 명령어>

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main|MERGING)
$ git merge --abort

 

6. Accept Current Change 선택 후 병합 진행

  • 충돌 해결 후에 git add . 명령을 사용하여 변경 사항 스테이징 영역 추가
  • "tigers.yaml"에 대한 해결 완료
  • conflict-1 브랜치의 변경 사항이 main 브랜치로 병합
admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git merge conflict-1
Auto-merging tigers.yaml
CONFLICT (content): Merge conflict in tigers.yaml
Automatic merge failed; fix conflicts and then commit the result.

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main|MERGING)
$ git add .

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main|MERGING)
$ git commit
[main f68c767] Merge branch 'conflict-1'

 

7. main 브랜치에서 conflict-2와 병합 진행

  • 병합 충돌이 발생하지 않고 자동 병합
admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git merge conflict-2
Auto-merging leopards.yaml
Merge made by the 'ort' strategy.
 leopards.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

 

8. 병합 진행 후 동기화 작업 진행

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git switch conflict-2
Switched to branch 'conflict-2'

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-2)
$ git rebase main
Successfully rebased and updated refs/heads/conflict-2.
>> conflict-2를 main 브랜치와 동기화

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-2)
$ git status
On branch conflict-2
nothing to commit, working tree clean

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (conflict-2)
$ git switch main
Switched to branch 'main'

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git status
On branch main
nothing to commit, working tree clean

 

9. 브랜치 삭제

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git branch -d conflict-1
Deleted branch conflict-1 (was 7f1f0e4).

admin@DESKTOP-4M8LFU2 MINGW64 ~/Desktop/git-basic (main)
$ git branch -d conflict-2
Deleted branch conflict-2 (was 19d426b).