유승훈

Git - 깃허브와 로컬 폴더 연결하기(error: remote contains you do not have locally) 본문

languages/Git&Github

Git - 깃허브와 로컬 폴더 연결하기(error: remote contains you do not have locally)

seunghuni96 2020. 11. 26. 14:23

컴퓨터의 로컬 폴더와 깃허브의 Repository를 연결하는 방법에는 두 가지 방식이 있습니다.

 

1. 로컬 폴더에서 git bash로 깃허브의 Repository와 연결

2. 깃허브의 Repository에서 내 컴퓨터로 Clone하는 방법

 

1. 로컬 폴더에서 git bash로 깃허브의 Repository와 연결

깃허브의 Repository에서 주소를 복사합니다. 그 다음 업로드하고자 하는 로컬 폴더에서 Git Bash를 엽니다.

git init
git add <Route to add>
git commit -m <Commit Message>
git remote add origin <Repository URL>
git push origin master

하지만 이렇게 Push를 하면,

Error: failed to push some refs to <Repository URL>

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.

이런 오류 메세지가 뜨면서 push가 되지 않습니다. 원격 저장소가 로컬에 없는 파일을 가지고 있기 때문입니다.

이는 Github에서 Repository를 만들때 README를 추가했기 때문인데요.

README를 지운 뒤에 다시 Push를 해 보면,

 

제대로 잘 Push 되는것을 볼 수 있습니다. 하지만 일반적으로 Repository에서 README를 많이 사용합니다. 

따라서 README 없이 repository를 만들고, 로컬 폴더에서 push해준 뒤, README를 add해주고 로컬 폴더에서 pull하는 방법을 활용할 수 있겠습니다.

 

2. 깃허브의 Repository에서 내 컴퓨터로 Clone 하는 방법

이렇게 깃허브의 저장소에서 clone하는 방법도 있습니다. 나중에 변경사항이 생긴다면, commit후 push하면 깃허브의 저장소를 업데이트 할 수 있습니다.

Comments