git merge troubleshooting

rg.log·2022년 1월 8일
0
post-thumbnail

슬기로운 협업을 위한 git error 대처법

내 branch에서의 변경사항을 협업 branch로 commit하고 push 하려할 때 이런 메세지를 볼 수 있다.

% git status
On branch be-feature
Your branch and 'origin/be-feature' have diverged,
and have 2 and 4 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .vscode/
        flask-server/__init__.py
        flask-server/views/__pycache__/
        flask-server/views/index.py
        venv/
be-feature branch가 분기되어 있고, 각각 2개, 4개의 다른 commit이 있다.
(git pull 명령어를 사용해 원격 branch로부터 local로 merge하라)

왜 이런 일이 일어난걸까?

보다시피 B를 시작으로 내 branch와 협업 branch가 갈라지기 시작했다.
해서 내 branch와 협업 branch는 각각 4와 2의 commit을 갖고 있다는 메세지가 완전히 설명된다.

이 문제는 일반적으로 be-feature branch(협업 branch)와 내 branch를 동기화하여 작업을 진행한 후
협업 branch로 변경 사항을 merge하기 전에
다른 팀원이 변경사항을 적용했을 때 발생한다.

어떻게 해결할 수 있을까?

두 가지가 있는데, 하나는 rebase이고 하나는 merge이다.

1. rebase

git rebase origin/be-feature

위 코드를 적용하면 아래 그림과 같이 된다.

내 branch는 이제 F를 시작으로 변경 사항이 존재한다.
이제, 협업 branch로 내 branch의 변경사항을 merge하는데 문제가 없을 것이다!

장점 : 깔끔한 선형 git history로 남아 버그 추적, 변경 사항을 되돌려야할 때 유용하다.

단점 : 잘못하면 commit history를 잃을 수 있는 merge conflict가 더 잦아질 수 있다.

2. merge

git merge origin/be-feature

위 코드를 적용하면 아래 그림과 같이 된다.

보다시피, 내 branch와 협업 branch가 갈라졌지만, 결국 하나로 합쳐졌다.

장점 : 충돌을 보다 쉽고 빠르게 해결할 수 있다.

단점 : 비선형 git history로 남아 때로는 history를 이해할 수 없는 지경에 이르게 된다.


나는 개인적으로 rebase를 많이 사용했다

%git pull origin be-feature --rebase

From 원격 저장소
 * branch            be-feature -> FETCH_HEAD
Successfully rebased and updated refs/heads/be-feature.

한 후에
git push 하니 원격 저장소에 합쳐진 형태로 성공적으로 push 되었다!

0개의 댓글