블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

2013. 1. 13. 20:40 Git, GitHub

Git clone후에 두명 이상이 작업을 한다. 그리고 같은 파일을 수정하여 Remote 저장소에 Push 할 경우 merge conflict가 발생한다. 이를 해결하기 위한 과정을 알아보자 


1) merge conflict 조건

  - 한명이 같은 파일을(예, README.md) 수정하고 GitHub(remote repository)에 push하였다. 

  - 다른 사람이 README.md 파일 내역을 수정하고 GitHub에 push 하려고 한다. 

  - 이때 git은 fail 메시지를 뱃으면서 push 되지 않는다. 

   

  - github에 한명이 push하여 분홍색으로 있고, gregg는 local 저장소에 commit된 내역을 push 하려고 할 경우


2) merge conflict 해결하기 

  - push : GitHub에 push하려 했더니 conflict가 발생함 

  - pull : GitHub 변경내용을 local 저장소와 merge 하기 

  - edit : 충돌나는 부분이 있다면 직접 편집하기 

  - commit -a : 다시 모든 파일을 commit 하기 

  - push : 변경된 내역을 다시 GitHub에 push하기 

/////////////////////////////////////////////////////////////////////

// 이미 github에는 README.md 파일이 수정되어 있음 (위 그림의 분홍색)

// "It was changed in Github directly by yun dowon" 메시지 commit

///////////////////////////////////////////////////////////////////////////////////////

// README.md  파일에 

// "checkout merge conflict with same file when another change it differently." 내용을 추가함

// (위 그림의 초록색 gregg로 보면 됨)

$ cat README.md

pro_git

=======


test pro git books

checkout merge conflict with same file when another change it differently.


// 추가된 내용을 commit 함 

$ git commit -am "change README.md"

[master fbf9382] change README.md

 1 file changed, 2 insertions(+), 1 deletion(-)


/////////////////////////////////////

// GitHub에 반영할려고 함 

$ git push

Username for 'https://github.com':

Password for 'https://ysyun@yuwin.co.kr@github.com':

To https://github.com/ysyun/pro_git.git

 ! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to 'https://github.com/ysyun/pro_git.git'

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')

hint: before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.


////////////////////////////////////////////////

// pull 을 수행 

$ git pull

remote: Counting objects: 5, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 3 (delta 1), reused 0 (delta 0)

Unpacking objects: 100% (3/3), done.

From https://github.com/ysyun/pro_git

   9ae6fb2..c917ae5  master     -> origin/master

Auto-merging README.md

CONFLICT (content): Merge conflict in README.md

Automatic merge failed; fix conflicts and then commit the result.


// 현재 상태를 확인

$ git status

# On branch master

# Your branch and 'origin/master' have diverged,

# and have 1 and 1 different commit each, respectively.

#

# Unmerged paths:

#   (use "git add/rm <file>..." as appropriate to mark resolution)

#

#       both modified:      README.md

#

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


////////////////////////////////////////////////////////////////

// README.md 파일이 GitHub내용과 Local 저장소 내용이 합쳐졌다

$ cat README.md

pro_git

=======


test pro git books

<<<<<<< HEAD

checkout merge conflict with same file when another change it differently.  <-- Local 저장소 내역

=======

It was changed in GitHub directly by yun dowon  <-- Remote 저장소 내역

>>>>>>> c917ae5f7787844b63826a460d82a998bad08a7f 


///////////////////////////////////////////

// 변집기를 통하여 직접 내용을 적절히 수정함

$ vi README.md

$ git status

# On branch master

# Your branch and 'origin/master' have diverged,

# and have 1 and 1 different commit each, respectively.

#

# Unmerged paths:

#   (use "git add/rm <file>..." as appropriate to mark resolution)

#

#       both modified:      README.md

#

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


///////////////////////

// 다시 commit을 한다 

$ git commit -a

[master f42a865] Merge branch 'master' of https://github.com/ysyun/pro_git


UserXP@NUKNALEA /d/Git_repositories/pro_git (master)

$ git status

# On branch master

# Your branch is ahead of 'origin/master' by 2 commits.

#

nothing to commit (working directory clean)


/////////////////////////////////////////////////

// conflict가 해결되었으면 다시 push 하여 통합한다

$ git push

Username for 'https://github.com':

Password for 'https://ysyun@yuwin.co.kr@github.com':

Counting objects: 10, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (6/6), done.

Writing objects: 100% (6/6), 734 bytes, done.

Total 6 (delta 3), reused 0 (delta 0)

To https://github.com/ysyun/pro_git.git

   c917ae5..f42a865  master -> master


////////////////////////

GitHub에 push된 내용 


3) merge conflict 시에 pull 의미 

  - git fetch : remote 브랜치 하나가 local 저장소로 내려온 것이고, local 저장소에 commit 된 상태는 아님 

  - git merge origin/master : local 저장소의 master와 origin/master 브랜치를 merge 함 

  - pull = git fetch + git merge origin/master 두개가 동시에 수행된 것임 

posted by 윤영식