Git conflict는 branch를 합치거나 pull 하는 과정에서
Git이 어떤 내용을 기준으로 합쳐야 할지 스스로 결정하지 못할 때 발생하는 충돌이다.
보통 같은 파일의 같은 부분을 서로 다르게 수정했을 때 발생한다.
보통 아래 상황에서 많이 발생한다.
git merge할 때git pull할 때git rebase할 때- 같은 파일의 같은 줄 근처를 서로 다르게 수정했을 때
예를 들어 main branch와 feature/login branch가 있다고 가정한다.
main에서는 아래처럼 수정했다.
button color: blue
feature/login에서는 같은 부분을 아래처럼 수정했다.
button color: red
이 상태에서 branch를 merge 하면
Git은 어느 내용을 남겨야 할지 자동으로 판단하지 못할 수 있다.
이럴 때 conflict가 발생한다.
현재 main branch에서 다른 branch를 merge 할 때 발생할 수 있다.
git switch main
git merge feature/loginAuto-merging app.css
CONFLICT (content): Merge conflict in app.css
Automatic merge failed; fix conflicts and then commit the result.충돌이 발생한 파일은 git status로 확인한다.
git statusOn branch main
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
both modified: app.css충돌이 난 파일을 열면 보통 아래처럼 표시된다.
<<<<<<< HEAD
button color: blue
=======
button color: red
>>>>>>> feature/login
<<<<<<< HEAD- 현재 branch 내용
=======- 내용 구분선
>>>>>>> feature/login- 합치려는 branch 내용
즉, 둘 중 무엇을 남길지 직접 정해야 한다.
충돌 파일을 열고 원하는 내용만 남기도록 직접 수정한다.
button color: blue
button color: red
button color: blue;
hover color: red;
중요한 점은 아래 표시를 전부 지워야 한다는 것이다.
<<<<<<<
=======
>>>>>>>
파일을 직접 수정한 뒤에는 다시 staging 하고 commit 해야 한다.
git add .
git commit -m "MERGE: resolve conflict"git switch main
git merge feature/login
git status충돌 파일 수정 후:
git add .
git commit -m "MERGE: resolve conflict"원격 저장소 내용을 가져오면서 merge 될 때도 conflict가 날 수 있다.
git pull origin main이 경우도 해결 방식은 같다.
git status로 충돌 파일 확인- 파일 열어서 직접 수정
git addgit commit
rebase 도 conflict가 날 수 있다.
git rebase main충돌 해결 후에는 아래처럼 진행한다.
git add .
git rebase --continuegit rebase --abortconflict가 난 merge 자체를 취소하고 싶으면 아래 명령을 쓴다.
git merge --abortmerge 도중 충돌이 났을 때, 해결하지 않고 merge 전 상태로 돌아간다.
오래 안 가져오면 서로 작업 차이가 커져서 conflict 확률이 올라간다.
git pull origin main특히 같은 함수, 같은 CSS, 같은 설정 파일은 충돌이 자주 난다.
branch를 오래 들고 있으면 merge 시 충돌이 많아진다.
git switch main
git pull origin main
git switch feature/login
git merge main현재 상태 확인:
git status충돌 해결 후 staging:
git add .merge 완료:
git commit -m "MERGE: resolve conflict"merge 취소:
git merge --abortrebase 계속:
git rebase --continuerebase 취소:
git rebase --abortgit status
git add .
git commit -m "MERGE: resolve conflict"git merge --abortgit add .
git rebase --continueGit conflict는
같은 부분을 서로 다르게 수정해서 Git이 자동으로 합치지 못하는 상태이다.
이 경우 파일을 직접 수정하고 git add 후 다시 commit 하거나,
rebase 중이면 git rebase --continue 하면 된다.