개발

[ERROR] [rejected] master -> master (fetch first)

ai4man 2023. 12. 21. 02:11
반응형

1. When

git push 실행 시, 원격 저장소와 로컬 저장소가 동기화되어 있지 않은 경우 데이터 손실을 막기 위해 발생.

 

2. Error Message

git push origin master
Username for 'https://github.com': johndoe
Password for 'https://johndoe@github.com':
To https://github.com/johhdoe/whatever.git
 ! [rejected]        master -> master (fetch first)
error: 레퍼런스를 'https://github.com/johndoe/whatever.git'에 푸시하는데 실패했습니다
힌트: Updates were rejected because the remote contains work that you do not
힌트: have locally. This is usually caused by another repository pushing to
힌트: the same ref. If you want to integrate the remote changes, use
힌트: 'git pull' before pushing again.
힌트: See the 'Note about fast-forwards' in 'git push --help' for details.

 

3. Solution

a. 동기화 후 다시 push

git pull origin master
git push origin master

 

b. [a. 동기화 후 다시 push] 방법 중 git pull origin master 단계에서 (병합) 오류 발생 시

 

다음과 같은 Error Message가 발생할 수 있다.

이 경우 병합 혹은 리베이스를 통해 git branch를 통합하는 방식으로 해결할 수 있다.

# Error Message
git pull origin master
remote: Enumerating objects: 1820, done.
remote: Counting objects: 100% (748/748), done.
remote: Compressing objects: 100% (151/151), done.
remote: Total 1820 (delta 639), reused 597 (delta 597), pack-reused 1072
오브젝트를 받는 중: 100% (1820/1820), 28.96 MiB | 8.89 MiB/s, 완료.
델타를 알아내는 중: 100% (934/934), 완료.
https://github.com/johndoe/whatever URL에서
 * branch            master     -> FETCH_HEAD
 * [새로운 브랜치]   master     -> origin/master
힌트: You have divergent branches and need to specify how to reconcile them.
힌트: You can do so by running one of the following commands sometime before
힌트: your next pull:
힌트:
힌트:   git config pull.rebase false  # merge
힌트:   git config pull.rebase true   # rebase
힌트:   git config pull.ff only       # fast-forward only
힌트:
힌트: You can replace "git config" with "git config --global" to set a default
힌트: preference for all repositories. You can also pass --rebase, --no-rebase,
힌트: or --ff-only on the command line to override the configured default per
힌트: invocation.
fatal: Need to specify how to reconcile divergent branches.

 

b-1. 병합(Merge)

git config pull.rebase false
git pull origin master
git push origin master

병합 (Merge) 도식화, Ref: https://www.atlassian.com/ko/git/tutorials/merging-vs-rebasing

 

로컬 저장소와 원격 저장소의 변경사항을 병합해 하나의 통합된 커밋을 생성.

브랜치의 그래프가 비선형적이다.

히스토리가 유지된 상태로 병합됨.

 

b-2. 리베이스(Rebase)

git config pull.rebase true
git pull origin master
git push origin master

리베이스 (Rebase) 도식화, Ref: https://www.atlassian.com/ko/git/tutorials/merging-vs-rebasing

 

브랜치의 그래프가 선형적임.

히스토리를 재작성하게 됨.

브랜치가 생성될 때의 기준점이 교체된다. 따라서 히스토리도 재작성되는 것임.

 

4. Reference

Git Instruction Page

https://git-scm.com/docs/user-manual#forcing-push

 

Merge vs Rebase

https://www.atlassian.com/ko/git/tutorials/merging-vs-rebasing

반응형