cd $HOME/lab
rm -rf test_merge_01
git init test_merge_01
cd test_merge_01머지
머지란
- 두 개의 브랜치를 하나로 합치는 것(병합)
merge 명령
- merge 명령은 방향성을 가짐
- 현재 브랜치(브랜치 A)에서 브랜치 B를 머지하면
- 브랜치 A에는 브랜치 B의 내용이 반영된 새로운 커밋이 생성
- 브랜치 B는 변화없음
merge 명령 사용법
git merge <머지할 브랜치>: <머지할 브랜치>를 현재 브랜치에 병합
merge 명령의 옵션
머지시의 파일 변화
- 양쪽 브랜치 중 하나에서만 수정이 발생한 경우, 수정 파일을 유지
- 양쪽 브랜치에서 모두 수정이 발생한 경우, 파일을 비교하여
- 같은 위치가 아닌 수정은 양쪽 모두 반영
- 같은 위치의 수정이 있는 경우 해당 부분에 대해 충돌
| 브랜치 A | 브랜치 B | 머지 결과 |
|---|---|---|
| 수정없음 | 수정없음 | 그대로 유지 |
| 수정 | 수정없음 | 브랜치 A의 수정 반영 |
| 수정없음 | 수정 | 브랜치 B의 수정 반영 |
| 수정 | 수정 | 수정 내역에 따라 두 가지 수정이 모두 반영되거나 충돌 |
- 충돌의 세부적인 내용과 해결 방법은 다음 절에서 설명
실습 1: 파일의 신규 생성이나 삭제가 없는 경우
- 레포지토리 생성
Initialized empty Git repository in ~/lab/test_merge_01/.git/
- 3개의 파일 생성
echo "file1 line 1" > file1.txt
echo "file2 line 1" > file2.txt
echo "file3 line 1" > file3.txt
git add .
git commit -m "c1"[main (root-commit) 6ffb8f1] c1
3 files changed, 3 insertions(+)
create mode 100644 file1.txt
create mode 100644 file2.txt
create mode 100644 file3.txt
- branch A 생성
git branch A- 브랜치 A로 전환
git switch ASwitched to branch 'A'
- file2.txt 수정
echo "file2 line 1 modified" > file2.txt
git add .
git commit -m "c2"[A d65f994] c2
1 file changed, 1 insertion(+), 1 deletion(-)
- main 브랜치로 전환
git switch mainSwitched to branch 'main'
- file3.txt 수정
echo "file3 line 1 modified" > file3.txt
git add .
git commit -m "c3"[main b6e62b1] c3
1 file changed, 1 insertion(+), 1 deletion(-)
- 현재 브랜치 상태
git log --oneline --graph --all --decorate* d65f994 (A) c2
| * b6e62b1 (HEAD -> main) c3
|/
* 6ffb8f1 c1
- main 브랜치에서 A 브랜치 머지
git merge A -m "c4"Merge made by the 'ort' strategy.
file2.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
- main 브랜치에는 c4 라는 머지 커밋이 생성
- A 브랜치는 그대로 변화없음
git log --oneline --graph --all --decorate* 8273964 (HEAD -> main) c4
|\
| * d65f994 (A) c2
* | b6e62b1 c3
|/
* 6ffb8f1 c1
- 머지 결과 아무데서도 수정하지 않은 것은 그대로 유지
cat file1.txtfile1 line 1
- A 브랜치에서만 수정한 경우 수정한 파일이 남음
cat file2.txtfile2 line 1 modified
- main 브랜치에서만 수정한 경우 수정한 파일이 남음
cat file3.txtfile3 line 1 modified
파일이 신규로 생성되는 경우
- 양쪽 브랜치 중 하나에서만 신규 파일이 생성된 경우, 신규 파일을 반입
- 양쪽 브랜치에서 모두 신규 파일이 생성된 경우, 파일을 비교하여
- 같은 위치가 아닌 내역은 양쪽 모두 반영
- 같은 위치의 내역은 경우 해당 부분에 대해 충돌
| 브랜치 A | 브랜치 B | 머지 결과 |
|---|---|---|
| 신규 생성 | 생성 없음 | 신규 파일 반입 |
| 생성 없음 | 신규 생성 | 신규 파일 반입 |
| 신규 생성 | 신규 생성 | 파일 내용에 따라 두 파일이 모두 반영되거나 충돌 |
실습 2: 파일이 신규 생성되는 경우
- 레포지토리 생성
cd $HOME/lab
rm -rf test_merge_02
git init test_merge_02
cd test_merge_02Initialized empty Git repository in ~/lab/test_merge_02/.git/
- 1개의 파일 생성
echo "file1 line 1" > file1.txt
git add .
git commit -m "c1"[main (root-commit) 568caf2] c1
1 file changed, 1 insertion(+)
create mode 100644 file1.txt
- branch A 생성
git branch A- 브랜치 A로 전환
git switch ASwitched to branch 'A'
- file2.txt 생성
echo "file2 line 1" > file2.txt
git add .
git commit -m "c2"[A f844307] c2
1 file changed, 1 insertion(+)
create mode 100644 file2.txt
- main 브랜치로 전환
git switch mainSwitched to branch 'main'
- file3.txt 생성
echo "file3 line 1" > file3.txt
git add .
git commit -m "c3"[main 03e84d7] c3
1 file changed, 1 insertion(+)
create mode 100644 file3.txt
- 현재 브랜치 상태
git log --oneline --graph --all --decorate* 03e84d7 (HEAD -> main) c3
| * f844307 (A) c2
|/
* 568caf2 c1
- main 브랜치에서 A 브랜치 머지
git merge A -m "c4"Merge made by the 'ort' strategy.
file2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 file2.txt
- main 브랜치에는 c4 라는 머지 커밋이 생성
- A 브랜치는 그대로 변화없음
git log --oneline --graph --all --decorate* 85cfcc6 (HEAD -> main) c4
|\
| * f844307 (A) c2
* | 03e84d7 c3
|/
* 568caf2 c1
- 양쪽 브랜치에서 새로 만든 파일들이 모두 포함됨
lsfile1.txt file2.txt file3.txt
파일이 삭제되는 경우
- 양쪽 브랜치 중 어디든 한 쪽에서 파일이 삭제되면 모두 제거됨
| 브랜치 A | 브랜치 B | 머지 결과 |
|---|---|---|
| 유지 | 유지 | 유지 |
| 삭제 | 유지 | 제거 |
| 유지 | 삭제 | 제거 |
| 삭제 | 삭제 | 제거 |
실습 3: 파일이 삭제되는 경우
- 레포지토리 생성
cd $HOME/lab
rm -rf test_merge_03
git init test_merge_03
cd test_merge_03Initialized empty Git repository in ~/lab/test_merge_03/.git/
- 3개의 파일 생성
echo "file1 line 1" > file1.txt
echo "file2 line 1" > file2.txt
echo "file3 line 1" > file3.txt
git add .
git commit -m "c1"[main (root-commit) d8a249f] c1
3 files changed, 3 insertions(+)
create mode 100644 file1.txt
create mode 100644 file2.txt
create mode 100644 file3.txt
- branch A 생성
git branch A- 브랜치 A로 전환
git switch ASwitched to branch 'A'
- file2.txt 삭제
git rm file2.txt
git commit -m "c2"rm 'file2.txt'
[A f846ef2] c2
1 file changed, 1 deletion(-)
delete mode 100644 file2.txt
- main 브랜치로 전환
git switch mainSwitched to branch 'main'
- file3.txt 삭제
git rm file3.txt
git commit -m "c3"rm 'file3.txt'
[main 2d09372] c3
1 file changed, 1 deletion(-)
delete mode 100644 file3.txt
- 현재 브랜치 상태
git log --oneline --graph --all --decorate* f846ef2 (A) c2
| * 2d09372 (HEAD -> main) c3
|/
* d8a249f c1
- main 브랜치에서 A 브랜치 머지
git merge A -m "c4"Merge made by the 'ort' strategy.
file2.txt | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 file2.txt
- main 브랜치에는 c4 라는 머지 커밋이 생성
- A 브랜치는 그대로 변화없음
git log --oneline --graph --all --decorate* 10135c4 (HEAD -> main) c4
|\
| * f846ef2 (A) c2
* | 2d09372 c3
|/
* d8a249f c1
- 양쪽 브랜치에서 삭제한 파일들이 모두 제거됨
lsfile1.txt