[Java] 9. Git & SourceTree

Kyunghwan Ko·2022년 10월 1일
0

Java

목록 보기
11/14

main으로 브랜치 이름 변경 후 push시 발생하는 에러

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks push -v --tags --set-upstream origin main:main
Pushing to https://github.com/kyunghwan1207/git_practice.git
To https://github.com/kyunghwan1207/git_practice.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/kyunghwan1207/git_practice.git'


hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

에러 발생 원인 및 해결방안

  • local(local repository)의 데이터와 github 저장소(remote repository)에 있는 데이터가 일치하지 않을 때 발생한다.

  • 따라서 origin의 내용을 local repo로 pull받으면 된다.

💡 현재의 경우 아마 GitHub에서 repository 생성할 때 ReadMe.md 파일을 추가해서 발생했을 가능성이 크다. 따라서 이 뒤에 제시된 git bash에 CLI명령어를 입력해서 해결하는 것이 어려울 것 같다면 아직 초기 단계이기 때문에 다시 새로운 repository를 파서 source tree와 연동시킨 후 이전 첫 단계부터 차근차근 진행해보기 바란다. (따라서 git bash명령어가 미숙하다면 이 뒤의 내용보단 새로운 repository를 만들어 다시 step을 진행하는 것을 추천한다.)

다시 에러발생

하지만 source tree의 UI를 통해서 Pull 받을 때

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks fetch --no-tags origin

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks pull origin main
From https://github.com/kyunghwan1207/git_practice
 * branch            main       -> FETCH_HEAD

fatal: refusing to merge unrelated histories

이런에러가 발생할 수도 있다.
Stack Overflow 에서 제시한 것처럼
GIT Fatal : refusing to merge unrelated histories

git pull --allow-unrelated-histories origin master 

이렇게 CLI로 입력해줘야하는대

Sourcetree error when trying to pull from repository
댓글을 참고하듯이 souce tree UI에선 위 작업이 불가능하다고해서
이 경우 git bash command에서 직접 CLI로 실행해야합니다.

git pull --allow-unrelated-histories origin main
// a.txt, b.txt, c.txt를 hello-sourcetree에 추가 후
git add .
git commit -m "첫 번째 commit"
git push origin main

만약 push 할때

 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/kyunghwan1207/git_practice.git'

이와 같은 에러가 발생한다면

git push origin +main

으로 진행하면 강제 push가 되어서 에러없이 진행할 수 있습니다.

참고(Reference)

GIT Fatal : refusing to merge unrelated histories

Sourcetree error when trying to pull from repository

profile
부족한 부분을 인지하는 것부터가 배움의 시작이다.

0개의 댓글