원격지의 최신 내용을 pull 하지 않아서 push 안 됨 (failed to push some refs to 'https://github.com/userId/userProject.git')
상황
1. github에 repositories 만듬 ( readme.txt 파일 생성 옵션 체크 )
2. intellij에서 repositories 연결
3. 새 파일들 생성해서 commit and push
4. 오류
실제 로그
C:\Users\gitProject>git push origin master
To https://github.com/userId/userProject.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/userId/userProject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
원인
원격저장소의 readme.txt 파일을 로컬에 pull 하지 않고 새 파일들을 생성해서 연결이 안된 것
원격지의 최신 내용을 먼저 받아오고(pull), 작업한 내용과 합쳐서 push해야합니다.
해결
git pull origin main --allow-unrelated-histories
git push --set-upstream origin main
설명
1. git pull origin main --allow-unrelated-histories
push 전에 먼저 pull을 해서 병합해야 한다. 그런데 pull을 하면 error
refusing to merge unrelated histories
--allow-unrelated-histories 이 명령 옵션은 이미 존재하는 두 프로젝트의 기록(history)을 저장하는 드문 상황에 사용된다고 한다. 즉, git에서는 서로 관련 기록이 없는 이질적인 두 프로젝트를 병합할 때 기본적으로 거부하는데, 이것을 허용해 주는 것이다.
2. git push --set-upstream origin main
upstream 은 로컬과 연결된 원격 저장소를 의미한다. 처음에는 비어져 있어서 설정해줘야 한다. 즉, git push --set-upstream A B 이 명령은 A원격저장소의 B브랜치에 push 하겠다는 뜻 이다. 최초 설정을 했으면 git push B 라고만 해도 push가 잘 된다.
origin은 처음에 원격저장소를 가리키는 이름으로 자동 지정된다.
참고
https://blog.shovelman.dev/924
[삽잡이 :: GIT] 어랏 push가 왜 안되지
멋대로 샤샤샥 커밋을 하다가... 갑자기!push가 안된다면 어떻게 할 것인가... 너님이 뭘 잘못한지 스스로 돌아봐봐.... 장난이구요...원격 저장소에서 pull을 땡겨오지 않을 때면 아래와 같은 문제
blog.shovelman.dev
https://velog.io/@ddoobukk2/upstreamorigin-%EC%B0%A8%EC%9D%B4-git-push-upstream-error
upstream/origin 차이, git push upstream error
아래와 같이 브랜치 이름만 주었을 경우 no upstream 에러가 발생한다\\fatal: The current branch master has no upstream branch.To push the current branch and set the remote as u
velog.io