DevOps/GIT

github#13 git rebase 충돌해결

aliceintr 2020. 11. 26. 10:29
반응형

다른사람과 협업을 할 때에 Remote Repository 에 있는 소스코드는 어지간 하면 rebase 하지 않도록 주의하자.

만약 본인 것의 코드만 업데이트 혹은 rebase 하고 싶다면

먼저 git pull로 최신버전의 코드를 Remote Repository 에서 받아서 working directory 에서 작업을 commit 하면서 rebase 를 하도록한다.

모든 작업이 다 마친후에 git push를 해서 Remote Repository 에 코드를 업데이트 하면 좋을 것 같다.

 

충돌이 나는 상황을 만들어 보자.

 

1. [Master Branch] a.txt 파일을 commit 한다. - 첫번째 commit

 

파일 내용

aaaa
% vi a.txt 
% git add a.txt 
% git commit -m "First from Master"
[master (root-commit) c2eb83d] First from Master
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

 

 

 

2. [rebase Branch] a.txt 파일을 수정하고 commit을 한다 - 첫번째 commit

 

파일내용

aaaa
R1
% git checkout -b rebase
Switched to a new branch 'rebase'
% vi a.txt 
% git add a.txt                    
% git commit -m "R1"               
[rebase 6232dda] R1
 1 file changed, 1 insertion(+)

 

 

 

3. [Master Branch] a.txt 파일을 commit 한다 - Master 두번째 commit

 

파일내용

aaaa
M1
% git checkout master
Switched to branch 'master'
% vi a.txt           
% git add a.txt      
% git commit -m "M1" 
[master eef8c11] M1
 1 file changed, 1 insertion(+)

 

 

 

 

4. [rebase Branch] a.txt 파일을 수정하고 commit을 한다 - 두번째 commit

 

파일내용

aaaa
R2
% vi a.txt           
% git add a.txt 
% git commit -m "R2" 
[rebase e8498c2] R2
 1 file changed, 1 insertion(+), 1 deletion(-)

 

 

 

 

 

5. [rebase Branch] a.txt 파일을 수정하고 commit을 한다 - 세번째 commit

 

파일내용

aaaa
R3
% vi a.txt           
% git add a.txt
% git commit -m "R3"
[rebase 7fa34ba] R3
 1 file changed, 1 insertion(+), 1 deletion(-)

 

 

 

 

 

 

6. 이제 rebase를 해보도록 해보자

merging 하는 중에 coflict 가 R1 commit 에 났다

 

% git rebase master
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
error: could not apply 6232dda... R1
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 6232dda... R1


% git status
interactive rebase in progress; onto eef8c11
Last command done (1 command done):
   pick 6232dda R1
Next commands to do (2 remaining commands):
   pick e8498c2 R2
   pick 7fa34ba R3
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'rebase' on 'eef8c11'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
	both modified:   a.txt

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

이제 파일 에디터를 열어서 conflict 난 부분을 고치자.

aaaa
M1,R1

다시 rebase 해준다.

%git rebase --continue

[detached HEAD ee8ac62] Rebase 1st
 1 file changed, 1 insertion(+), 1 deletion(-)
Auto-merging a.txt

CONFLICT (content): Merge conflict in a.txt
error: could not apply e8498c2... R2
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply e8498c2... R2

이제는 R2랑 컨플릭트가 난다.

또 파일 에디터를 열어서 conflict 난 부분을 고치자.

 

aaaa
M1,R1,R2

다시 rebase

 % git rebase --continue
[detached HEAD f4690c6] Rebase 2nd
 1 file changed, 1 insertion(+), 1 deletion(-)
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
error: could not apply 7fa34ba... R3
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 7fa34ba... R3

이제는 R3랑 컨플릭트가 난다.

또 파일 에디터를 열어서 conflict 난 부분을 고치자.

aaaa
M1,R1,R2,R3
% git rebase --continue             
[detached HEAD 197f600] Rebase 3rd
 1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/rebase.

이제 충돌없이 잘 rebase 되었다.

 

 

 

 

 

 

 

 

 

내용이 도움이 되셨다면 블로그 구독하기 부탁드리겠습니다.

* 이 글의 모든 저작권은 aliceintr에 있으며 무단 배포 및 사용은 자제해 주시기 바랍니다. *

반응형

'DevOps > GIT' 카테고리의 다른 글

github#15 git pull request & git merge request 1편  (0) 2020.11.27
github#14 git project 잘 관리하기  (0) 2020.11.26
github#12 git rebase 기초  (0) 2020.11.26
github#11 git tag 와 branch 차이  (0) 2020.11.25
github#10 git push and pull  (0) 2020.11.24