[GitHub] 로컬 프로젝트를 git repository에 연결하기

EunchongKim·2023년 3월 10일
1

참고
먹세님의 블로그 - [GIT] 기존 프로젝트를 git repository에 연결 하기

다른 정보
fork한 repository 최신화
fork한 프로젝트 pr하기


step0. (mac os) cmd를 쓸 terminal이나 iterm을 켠다.

step1. 기존 프로젝트가 있는 directory로 이동

cd project

step2. 깃허브 주소 초기화 (이미 연동되어 있을 경우 오류 방지)

git init

연동되어 있는 경우 완료메시지: Reinitialized existing Git repository in /Users/사용자/project/.git/
연동되어 있지않은 경우 완료메시지: Initialized empty Git repository in /Users/사용자/project/.git/

step3. 프로젝트를 연결할 빈 repository 생성

깃허브(GitHub)나 bitbucket에서 파일 생성 없이(README 등) 빈 레포지토리로 생성하는 걸 권장.
README 파일을 생성하면 최초 커밋 시 파일 기록이 달라서 오류가 발생할 수 있다.

step4. 깃허브에 만든 repository 주소 연결

내 깃허브에 연결할 repository에서 <>code/clone/HTTPS주소 를 카피해서 붙이기

git remote add origin https://github.com/내깃허브주소/project.git

연결된 repository 확인하기(fetch/push 주소 확인)

git remote -v

완료메시지:
origin https://github.com/내깃허브주소/project.git (fetch)
origin https://github.com/내깃허브주소/project.git (push)

step5. 새 repository에서 pull 받아서 git history 동기화

git pull origin main

에러: fatal: couldn't find remote ref master
해결방법: [Git에러]fatal: couldn't find remote ref master 해결

step6. 새 repository에 파일 업로드를 위해 스테이지에 파일 추가

git add .

add . : 모든 파일을 스테이지에 올린다는 뜻. add 뒤에 특정 브랜치를 쓰면 그 브랜치 파일만 업로드 된다.

step7. 최초 커밋

git commit -m "first commit"

step8. repository에 파일 업로드

git push -u origin main

-u 옵션: 다음부터는 git pull / git push 를 할 때 origin master 를 지정하지 않아도 알아서 origin master로 붙어서 가져오라는 옵션

0개의 댓글