본문 바로가기

git & github

브랜치 병합

서로 다른 파일 병합하기

$ mkdir manual-2
$ cd manual-2/

$ git init
Initialized empty Git repository in C:/Users/test/manual-2/.git/

$ vi work.txt
1

$ git add work.txt
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory

$ git commit -m "work 1"
[master (root-commit) d3cf92b] work 1
 1 file changed, 1 insertion(+)
 create mode 100644 work.txt

$ git branch o2

[위의 명령어 결과]

$ vi master.txt
master 2

$ git add master.txt
warning: LF will be replaced by CRLF in master.txt.
The file will have its original line endings in your working directory

$ git commit -m "master work 2"
[master 6e23293] master work 2
 1 file changed, 1 insertion(+)
 create mode 100644 master.txt

[위 명령어 결과]

$ git checkout o2
Switched to branch 'o2'

$ vi o2.txt
o2 2

$ git add o2.txt
warning: LF will be replaced by CRLF in o2.txt.
The file will have its original line endings in your working directory

$ git commit -m "o2 work 2"
[o2 9748ae7] o2 work 2
 1 file changed, 1 insertion(+)
 create mode 100644 o2.txt

$ git log --oneline --branches --graph
* 9748ae7 (HEAD -> o2) o2 work 2
| * 6e23293 (master) master work 2
|/
* d3cf92b work 1

[위 명령어 결과]

o2에서 작업이 모두 끝났다 가정하고 o2브랜치를 master브랜치로 병합

$ git checkout master
Switched to branch 'master'

$ git merge o2
Merge made by the 'recursive' strategy.
 o2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 o2.txt

$ git log --oneline --branches --graph
*   19384b3 (HEAD -> master) Merge branch 'o2'
|\
| * 9748ae7 (o2) o2 work 2
* | 6e23293 master work 2
|/
* d3cf92b work 1

브랜치 병합 시에 편집기 안 열리게 하기 : —no-edit 옵션 추가

브랜치 병합 시에 커밋 메시지를 수정하기 : —edit 옵션 추가

 

[위 명령어 결과]

 

같은 파일의 다른 위치 수정하여 병합하기

$ mkdir manual-3
$ cd manual-3/
$ git init
Initialized empty Git repository in C:/Users/teset/manual-3/.git/

$ vi work.txt
#title
content


#title
content

$ git add work.txt
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory

$ git commit -m "work 1"
[master (root-commit) e9f154a] work 1
 1 file changed, 6 insertions(+)
 create mode 100644 work.txt

$ git branch o2

$ vi work.txt
#title
content
master content 2

#title
content

$ git commit -am "master work 2"
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
[master 9bdb1de] master work 2
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout o2
Switched to branch 'o2'

$ vi work.txt
#title
content


#title
content
o2 content 2

$ git commit -am "o2 work 2"
[o2 95a4b51] o2 work 2
 1 file changed, 1 insertion(+)

$ git checkout master
Switched to branch 'master'

$ git merge o2
Auto-merging work.txt
Merge made by the 'recursive' strategy.
 work.txt | 1 +
 1 file changed, 1 insertion(+)

$ cat work.txt
#title
content
master content 2

#title
content
o2 content 2

 

같은 파일의 같은 위치 수정하여 병합하기

깃은 줄 단위로 변경 여부를 확인함

각 브랜치에서 같은 파일이름을 가지고 있으면서 같은 줄을 수정했을 때,

브랜치를 병합하면 브랜치 충돌(conflict)이 발생

충돌이 생긴 파일은 자동으로 병합이 되지 않으므로,

사용자가 직접 충돌 부분의 해결한 후에 커밋

$ mkdir manual-4
$ cd manual-4
$ git init
Initialized empty Git repository in C:/Users/test/manual-4/.git/

$ vi work.txt
#title
content

#title
content

$ git add work.txt
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory

$ git commit -m "work 1"
[master (root-commit) 6e7f7fb] work 1
 1 file changed, 5 insertions(+)
 create mode 100644 work.txt

$ git branch o2

$ vi work.txt
#title
content
master content 2
#title
content

$ git commit -am "master work 2"
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
[master 8c85a24] master work 2
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout o2
Switched to branch 'o2'

$ vi work.txt
#title
content
o2 content 2
#title
content

$ git commit -am "o2 work 2"
[o2 631f306] o2 work 2
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout master
Switched to branch 'master'

$ git merge o2
Auto-merging work.txt
CONFLICT (content): Merge conflict in work.txt    #브랜치 충돌 오류
Automatic merge failed; fix conflicts and then commit the result.

$ vi work.txt
#title
content
<<<<<<< HEAD
master content 2    #현재 브랜치에서 수정한 내용
=======
o2 content 2    #병합한 브랜치에서 수정한 내용
>>>>>>> o2
#title
content

****내용 수정하기****

#title
content

master content 2
o2 content 2

#title
content

$ git commit -am "merge o2 branch"
[master 8d05db5] merge o2 branch

$ git log --oneline --branches --graph
*   8d05db5 (HEAD -> master) merge o2 branch
|\
| * 631f306 (o2) o2 work 2
* | 8c85a24 master work 2
|/
* 6e7f7fb work 1

 

병합 및 충돌 해결 프로그램

프로그램 이름 설명
P4Merge 무료, 사용 편리, 단축키 미지원
Meld 무료, 오픈소스, 파일 비교 및 직접 편집 가능
Kdiff3 무료, 사용 편리, 한글 깨짐 현상 있음
Araxis Merge 유료, 용량 큰 파일에서도 잘 동작

 

병합 끝난 브랜치 삭제

브랜치 병합 후, 사용하지 않는 브랜치는 삭제 가능

브랜치 삭제를 해도 브랜치가 완전히 지워지는 것이 아님

같은 이름의 브랜치를 다시 만들면 예전 내용 확인 가능

기본 브랜치가 master 이므로, 브랜치 삭제를 하려면 master에서 해야 함

$ git branch
* master
  o2

$ git checkout master

$ git branch -d o2    #o2브랜치 삭제
Deleted branch o2 (was 631f306).

'git & github' 카테고리의 다른 글

원격 저장소와 깃허브  (0) 2020.10.31
브랜치 관리  (0) 2020.10.31
깃과 브랜치  (0) 2020.10.31
Git으로 버전 관리하기 2  (0) 2020.10.31
Git으로 버전 관리하기 1  (0) 2020.10.31