본문 바로가기

Note/생활코딩 GIT

[생활코딩_GIT] 03. CLI Branch & conflict - #3 3way merge

#깃이 머지되는 방법 - 3 way merge

Branch here base there (결과물)
2 way merge
(결과물)
3 way merge
라인 1 A A A A A
라인 2 B B B ? H
라인 3 C C T ? T
라인 4 H D T ? ?

- 3 way merge 는 두 브랜치 라인 중 하나만 수정된다면,

해당 영역은 수정된 파일 기준으로 병합되는 것이다.

 

# 실습

82109@DESKTOP-OO924JS MINGW64 ~
$ cd git-test

82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ mkdir manual-merge

82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ cd manual-merge

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge
$ git init
Initialized empty Git repository in C:/Users/82109/git-test/manual-merge/.git/

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ nano work.txt

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ 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

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git commit -m "work 1"
[master (root-commit) aacf96f] work 1
 1 file changed, 7 insertions(+)
 create mode 100644 work.txt

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git branch here

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* aacf96f (HEAD -> master, here) work 1

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git checkout here
Switched to branch 'here'

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ nano work.txt

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ git commit -am "here work 2"
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
[here 4cd4585] here work 2
 1 file changed, 2 insertions(+), 2 deletions(-)

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ git log --all --graph --oneline
* 4cd4585 (HEAD -> here) here work 2
* aacf96f (master) work 1

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ git checkout master
Switched to branch 'master'

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git branch there

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* 4cd4585 (here) here work 2
* aacf96f (HEAD -> master, there) work 1

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git checkout there
Switched to branch 'there'

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (there)
$ nano work.txt

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (there)
$ git log --all --graph --oneline
* 4cd4585 (here) here work 2
* aacf96f (HEAD -> there, master) work 1

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (there)
$ git commit -am "there work 2"
[there 6aa080b] there work 2
 1 file changed, 2 insertions(+), 2 deletions(-)

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (there)
$ git log --all --graph --oneline
* 6aa080b (HEAD -> there) there work 2
| * 4cd4585 (here) here work 2
|/
* aacf96f (master) work 1

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (there)
$ git checkout here
Switched to branch 'here'

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ git merge there
Auto-merging work.txt
CONFLICT (content): Merge conflict in work.txt
Automatic merge failed; fix conflicts and then commit the result.

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ git merge tool
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ git status
On branch here
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:   work.txt

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

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ git mergetool

This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc codecompare smerge emerge vimdiff nvimdiff
Merging:
work.txt

Normal merge conflict for 'work.txt':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (p4merge):


82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ git status
On branch here
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        modified:   work.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        work.txt.orig


82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ cat work.txt.orig
A

H

T

<<<<<<< HEAD
H
=======
T
>>>>>>> there

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ cat work.txt
A

H

T

D, T, H

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ rm work.txt.orig

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here|MERGING)
$ git commit
[here c8f3323] Merge branch 'there' into here

 

1. git mergetool

- 병합을 전문적으로 해주는 도구

 

2. git staus

- 저 tool이 add 까지 해줌.

 

3. cat work.txt.org

- original 머지 내용을 백업해놓을것

 

4. cat work.txt

-프로그램에서 수정한 내용

 

5. rm work.txt.org

- work.txt.org 라는 이름의 파일을 삭제 (수정된 내용이 맞다면)

 

 

 

 

 

# git 에서 p4merge 사용하기

1. 하단 링크에서 p4merge 를 다운로드 한다.

https://www.perforce.com/downloads/visual-merge-tool

 

Download Helix Visual Merge Tool Today | Perforce

Helix Visual Merge Tool (P4Merge) is a three-way merging and side-by-side file comparison tool. Use it to visualize your merges, obtain comprehensive file history, and compare a broad range of image files. Download the tool to get started.

www.perforce.com

 

2. 셋팅을 하기 위해서 git bash를 실행한다.

 

3. git config --global merge.tool p4mergetool 를 입력하면 기본 머지툴로 설정이 된다. 

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ git config --global merge.tool p4mergetool

4. $ cat ~/.gitconfig 를 입력하여, 잘 설치되었는지 확인하자.

현재 tool = p4mergetool로 잘 설정되어있다.

82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (here)
$ cat ~/.gitconfig
[user]
        name = Jang.hyunjeong
        email = jang.hyunjeong01@gmail.com
[credential]
        helper = manager-core
[core]
        editor = nano
[merge]
        tool = p4mergetool