본문 바로가기
Git

Git branch & conflict(5)

by GalaxyStar 2024. 2. 19.

Git branch here

$git branch here
$git log --all --graph --oneline
* 0bd18f7 (HEAD -> master, here) ABCD

Branch here 생성.

 

Git checkout here

$git checkout here
switched to branch 'here'
$git log --all --graph --oneline
* 0bd18f7 (HEAD -> here, master) ABCD

Branch here 전환

 

work.txt 수정

 

Git commit -am "AHCH"

$git add work.txt
$git commit -m "AHCH"
[here 78cd700] AHCH

 

Git checkout master

$git checkout master
Switched to branch 'master'
$git log --all --graph --oneline
* 78cd700 (here) AHCH
* 0bd18f7 (HEAD -> master) ABCD

 

Git branch there

$git branch there
$git log --all --graph --oneline
* 78cd700 (here) AHCH
* 0bd18f7 (HEAD -> master, there) ABCD

 

Git checkout there

$git checkout there
Switched to branch 'there'
$git log --all --graph --oneline
* 78cd700 (here) AHCH
* 0bd18f7 (HEAD -> there, master) ABCD

 

work.txt 수정

 

Git add work.txt

Git commit -m "ABTT"

$git add work.txt
$git commit -m "ABTT"
[there c97d1e7] ABTT
$git log --all --graph --oneline
* c97d1e7 (HEAD -> there) ABTT
| * 78cd700 (here) AHCH
|/
* 0bd18f7 (master) ABCD

 

Branch here에서 branch there merge 해보도록 한다.

Git checkout here

$git checkout here
Switched to branch 'here'
$git log --all --graph --oneline
* c97d1e7 (there) ABTT
| * 78cd700 (HEAD -> here) AHCH
|/
* 0bd18f7 (master) ABCD

 

Git merge there

git-merge
git-merge

Branch here에서 branch there merge conflict 발생.

 

3 way merge에서 자동으로 처리할 없는 빨간색 부분을 표시해놓음.

빨간색 부분을 직접 수정할수도 있지만 병합을 전문적으로 해주는 외부도구를 이용할수도 있다.

 

병합을 도와주는 외부 도구는 다음과 같다.

 

P4merge tool 다운로드

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

 

Git config --global merge.tool p4mergetool

Git 전역 설정으로 merge tool p4 merge tool 쓰겠다라고 지정한다는 .

$git config --global merge.tool p4mergetool

홈디렉토리 아래에 .gitconfig hidden file 존재하고 git 파일에 적혀있는 내용을 참고해서 어떻게 동작할지를 결정한다.

p4mergetool
p4mergetool

그러나 .gitconfig 안에는 p4mergetool 경로가 적혀 있지 않으므로...

 

Apple: git config --global mergetool.p4mergetool.cmd \

"/Applications/p4merge.app/Contents/Resources/launchp4merge \$PWD/\$BASE \$PWD/\$REMOTE \$PWD/\$LOCAL \$PWD/\$MERGED"

 

윈도우 p4merge 설치 방법

 

Setup p4merge as difftool and mergetool on Windows

Setup p4merge as difftool and mergetool on Windows - p4merge-git-tool.md

gist.github.com

Windows: git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'

$git config --global mergetool.p4merge.path "C:\Program Files\Perforce\p4merge.exe'

 

.gitconfig 파일 확인시, p4merge tool 경로가 세팅되어 있는것을 확인.

명령어 실행후 .gitconfig 파일 열었을때 mergetool 추가 되어 있음을 확인.

p4merge
p4merge

Git mergetool

실행시, p4merge tool 나옴

git-mergetool
git-mergetool
git-mergetool
git-mergetool

- LOCAL: 현재 내가 속해있는 branch

- REMOTE: 땡겨 오려는 branch

- BASE: LOCAL, REMOTE 조상.

- merge 하려는 파일이 work.txt 된다.

- work.txt 내에 DTH conflict 발생한 부분이므로 merge tool 어떻게 해줄수 없기 때문에 직접 "우리보고 직접 수정해주세요!" 라고 하는 부분.

git-mergetool
git-mergetool

work.txt Conflict 부분인 D,T,H 위와 같이 수정후 저장버튼 클릭후 프로그램을 끄게되면 아래와 같이 자동으로 staging area add까지 해준다.

 

work.txt.org: 이전 상태를 백업해 놓은 것임.

 

work.txt

변경된 내용이 추가 되어 있음.

Work.txt 확실하면 백업해놓음 work.txt.org 삭제하면 된다.

 

Git commit

$git commit

 

Commit이 잘 되었음을 확인

$git log --all --graph --oneline
*   a5419232 (HEAD -> here) Merge branch 'there' into here
| \
| * c97d1e7 (there) ABTT
* | 78cd700 AHCH
|/
* 0bd18f7 (master) ABCD

 

Branch라는 기능은 git 등장하기 이전부터 있었습니다.

하지만 비효율적이고 위험했어요. 그래서 branch 사용되지 않았습니다.

 

Git branch 여러 효율성을 획기적으로 극복했습니다.

Branch 서서히 사용자들의 생활로 스며들기 시작했습니다.

 

가벼운 기능을 개발할때도 branch 새로 만들어서 거기서 버전을 만든 다음에 완성한 다음에 필요 없으면 버리고 필요하면 마스터로 merge 후에 버리는 것이죠.

 

Branch 버릴수 있는 작업을 만들 있는 최선의 도구가 된것 입니다.

'Git' 카테고리의 다른 글

Git branch & conflict(7)  (0) 2024.02.19
Git branch & conflict(6)  (0) 2024.02.19
Git branch & conflict(4)  (0) 2024.02.18
Git branch & conflict(3)  (0) 2024.02.18
Git branch & conflict(2)  (1) 2024.02.18