충돌 문제 해결 - CONFLICT : 협업하는 두 개발자 동일한 파일의 동일한 라인 수정하여 충돌 발생 시키기

CODEDRAGON Development/Git, PM

반응형

 

  

 

충돌 문제 해결 - CONFLICT : 협업하는 개발자 동일한 파일의 동일한 라인 수정하여 충돌 발생 시키기

·       협업시 가장 빈번히 발생하고 가장 골치 아픈 충돌문제를 해결하는 방법을 확인하겠습니다.
·       개발자들이 각자 같은 코드의 같은 행을 수정하면 충돌이 일어나고 마지막에 push하는 사람이 충돌을 해결해야 합니다.

 

 

 

 


 

 

 

 

A - index.html 파일에 같은 코드의 같은 수정하기

 

<h1> 태그를 <h3> 변경합니다.

 

 

 

 

B- index.html 파일에 같은 코드의 같은 수정하기

Collaboration 문구를 <strong> 태그로 감사 줍니다.

 

<h1><strong>Collaboration</strong> Work</h1>

 

 

 

 

A - 새로운 버전 생성하기

git commit -am 'modify <h1> -> <h3>'

 

 

 

git log

 

 

 

 

B- 새로운 버전 생성하기

git commit -am 'add <strong>tag'

 

 

git log

 

 

 

 

A - 원격 저장소에 push

git pull

 

 

 

git push

 

 

 

 

B- 원격 저장소에 push

developerB 간만의 차이로 늦게 'git pull' 하게 되면 B에서 충돌메시지가 발생합니다.

git pull

 

 

CONFLICT라는 메시지가 출력됩니다.

codedragon@CODEMASTER MINGW64 /c/CodeLab/gitLab/coworkB (master)
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/codeconn/worktogether
   5ec5b78..2fa313a  master     -> origin/master
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
 
codedragon@CODEMASTER MINGW64 /c/CodeLab/gitLab/coworkB (master|MERGING)
$
 

 

'git status' 명령으로 어디에 충돌이 났는지 확인할 있습니다.

git status

 

 

 

'git status' 확인해보면 "Unmerged paths:" "both modified: index.html" 메시지로

'index.html'파일이 충돌이 되었다는 것을 확인할 있습니다.

codedragon@CODEMASTER MINGW64 /c/CodeLab/gitLab/coworkB (master|MERGING)
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
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:   index.html
 
no changes added to commit (use "git add" and/or "git commit -a")
 
codedragon@CODEMASTER MINGW64 /c/CodeLab/gitLab/coworkB (master|MERGING)
$
 

 

 

 

B - 충돌 해결하기

충돌난 파일을 오픈합니다.

 

 

에디터로 해당파일을 열어보면 어떤 부분에서 충돌이 났는지 확인할 있습니다.

<<<<<<< HEAD라고 표시된 부분은 developerB 작성한 코드 이고

=======밑에 표시된 부분은 원격저장소에서 가져온 코드입니다.

 

 

developerB 보기에 변경된 부분 모두 필요하다고 판단되면

모두 적용하여 충돌부분을 적절하게 수정을 합니다. 그리고 충돌이 일어났다고 표시한 충돌기호를 모두 삭제한 파일을 저장합니다.

해당 충돌을 해결하기 위해 부분을 수정한 개발자와 수정을 했는지 충분히 협의를 해당 충돌부분을 적절히 수정합니다.

 

 

 

충돌이 해결되었음을 git에게 알려주기

충돌이 해결되었음을 git에게 알려주기위해 다시 'git add'명령을 수행하면 됩니다.

git add 'index.html'

 

 

 

git status

 

 

index.html 초록색으로 수정되었다라고 표시됩니다.

codedragon@CODEMASTER MINGW64 /c/CodeLab/gitLab/coworkB (master|MERGING)
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)
 
Changes to be committed:
 
        modified:   index.html
 
 
codedragon@CODEMASTER MINGW64 /c/CodeLab/gitLab/coworkB (master|MERGING)
$
 

 

 

 

B-충돌 해결한 새로운 버전 생성하기

'git commit -m' 커밋과 동시에 메시지를 작성해도 되지만 'git commit' 명령으로 충돌 해결에 대한 메시지도 함께 커밋하는 것이 좋습니다.

 

git commit

 

 

'git commit' 실행하면 git 충돌이 상황을 알고 있기 때문에 해당 내용을 포함한 commit메시지를 자동으로 작성해 줍니다.

 

 

여기에 여러분이 추가적인 메시지를 작성한 저장하면 commit 이루어집니다.

키보드에서 "i" 입력하여 편집모드로 바꾼 해결된 정보를 상세히 적어줍니다.

i

conflict solved (detailed info)

:wq

 

 

저장하고 바줘나오면 자동으로 commit 됩니다.

 

 

 

git log

 

 

 

충돌을 해결 했으므로 이제 push 있는 상태가 되었으므로 원격저장소에 변경사항을 push합니다.

git push origin master

 

 

 

만약 충돌을 해결하는 많은 시간이 걸렸다면 바로 git push 수행하는 것이 아니라 'git pull' 'git push' 수행합니다.

 

 

 

원격저장소 확인

 

 

 

A - 원격저장소의 변경사항을 로컬저장소로 가져오기

git pull

 

 

git log

 

 

 

cat index.html

 

 

 

 

 

 

 

B - index.html 오픈합니다.

 

cat index.html

 

 

 

 

developerA developerB 모두 충돌이 해결되었으므로 모두 동일한 내용의 파일을 확인할 있습니다.

developerA


 
developerB


 

개발환경에서는 보다 복잡하고 다양한 충돌 문제가 발생되며 충돌 해결을 많이 시간이 소요될 있습니다.

 

 

 

 

 

충돌을 막는 (git으로 협업시 )

변경사항이 발생하여 새로운 버전이 생성되면 즉시 push 하여 충돌을 막아주는 것이 가장 좋습니다.

http://codedragon.tistory.com/5592

 

충돌을 막는 팁(git으로 협업시 팁)

충돌을 막는 팁(git으로 협업시 팁) ·      변경사항이 발생하여 새로운 버전이 생성되면 즉시 push를 하여 충돌을 막아주는 것이 가장 좋습니다. ·      자주 그리고 주기적으로 원격저장소

codedragon.tistory.com

 

 

반응형