[Git] pull strategy 풀 전략

Madeline👩🏻‍💻·2023년 1월 23일
0

개발 지식

목록 보기
9/11

인생 첫 협업,,
평화롭게 충돌도 없이 팀원 코드 풀 받아오려고 하는데,
이런 에러가 뜬다.

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

해석해보면 여러가지 브랜치들이 있는데 이거 어떻게 합칠지 정해라, 라는 뜻

구글링해봐도 잘 정리되어 있는 글이 없어서 정리해본다.

힌트에 뜬 것처럼 ff only 랑 rebase 2가지 방법이 있다.

풀 하기 전 상황은 이렇다.

너가 브랜치 만들어서 작업하는 도중에
다른 팀원이 다른 원격 저장소에 새로운 브랜치와 새로운 commit을 만든거다.
이 때 git pull, git pull origin main 을 하면 위와 같은 에러가 발생한다고 한다.
내 로컬 브랜치에 원격 브랜치와 merge commit 되니까 발생한다고 한다.

1. git config pull.ff only

https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FQmskH%2FbtqOc9IcAtd%2FNrj3jr3U9SULqwWzhueLR1%2Fimg.png![](https://velog.velcdn.com/images/maddie/post/be7dc19a-9fc6-455b-86d8-c28b537d9308/image.png)

pull 하려는 원격저장소의 브랜치와 로컬저장소의 브랜치가 fast-forward 관계일 때만 pull을 허용한다.

두 브랜치가 fast-forward 관계라는 건 둘 중 하나를 의미한다.

두 브랜치가 갈라진 commit을 기준으로

  1. 로컬저장소에만 새로운 commit이 있고, 원격저장소에는 없다. = pull을 받아올 필요가 없음
  2. 원격저장소에만 새로운 commit이 있고, 로컬저장소에는 없다. = 이 때만 pull이 가능하다는 뜻이 된다.

그럼 만약 원격저장소의 새로운 commit이 존재하는데 git pull을 하지 않은 상태에서 로컬저장소에 새로운 commit을 했다면, git은 pull을 허용하지 않을 것이다.

Fast-Foward 관계란 앞선 커밋을 충돌(의미상 진짜 충돌,,겹치는거,,conflict 아님) 없이 따라갈 수 있는 관계를 말한다.

나의 로컬 저장소의 상태에 관계 없이 버전 기록이 변경되지 않을 거라는 확신을 가질 수 있다.
(merge commit 없음)

2. git pull --rebase

https://velog.velcdn.com/images/eunddodi/post/c993c875-552a-4117-a6e2-04cdf1d5732d/image.png![](https://velog.velcdn.com/images/maddie/post/a5e0ddd9-b854-4b69-9d68-bc2d2141203a/image.png)

rebase란? 새 브랜치가 시작된 분기점 commit이 존재한다. 이 분기점을 기준 브랜치의 가장 최근 commit으로 변경하는 작업.

=> 부주의하게 사용할 경우 별도의 알림 없이 git history를 영구적으로 변경할 수 있기 때문에 ff-only 방식을 더 추천한다고 한다.

🦁 쉬운 설명

스택오버플로우에 잘 설명되어있었음

그래서 둘의 차이가 뭐냐?

만약에 origin/main 브랜치에 A - B - C 순서로 commit 이 있고,
내 로컬 local/main 브랜치에 A - B - D 순서로 commit 이 있으면,

1) git pull --rebase

이건 아래와 똑같음

git fetch
git rebase origin/main

그래서 결론적으로는
A - B - C - D 순서로 적용될것임

2) git pull --ff-only

이건 이렇게 됨

git fetch
git merge --ff-only origin/main

이렇게 하면 에러날 듯.
이건 fast-forwarded 한 상황에만 적용되는 것이기 때문에,
(로컬엔 있고 원격엔 없던지, 원격에는 있고 로컬에는 없던지 하는 상황)
주어진 상황은 로컬에도, 원격에도 브랜치와 커밋이 있기 때문에 에러가 날 것임..

그래서 위에서 새로운 로컬 커밋 D 가 없었다면 1)과 2)의 결과가 똑같을 것이다.

🐶 reference

https://velog.io/@eunddodi/git-pull-시-발생하는-warning-해결하기Need-to-specify-how-to-reconcile-divergent-branches

https://sanghye.tistory.com/43

https://stackoverflow.com/questions/25430600/difference-between-git-pull-rebase-and-git-pull-ff-only

https://minemanemo.tistory.com/46

https://minikuma-laboratory.tistory.com/27

profile
Major interest in iOS 🍀 & 🍎

0개의 댓글