G's days

git 원격 저장소(github) 로컬과 연동하기(불러오기, 삭제, 확인) 본문

프로그래밍 공부

git 원격 저장소(github) 로컬과 연동하기(불러오기, 삭제, 확인)

Hi☆G 2021. 9. 4. 10:58

로컬 Git저장소 초기화 (로컬에Git저장소 만들기)

git init

 

확인

git status


로컬과 연동시키기

1)  빈 repository를 받아오기

2) 이미 작업중인 repository를 받아오기

 


1)  빈 repository를 받아오기

Readme 파일 생성

echo "# 깃저장소로 사용할 폴더명" >> README.md


현재 branch를 main branch로 이동

git branch -M main


원격 저장소에서 불러오기

git remote add [REMOTE_NAME] [REMOTE_GIT_URL]

-> (뒤에 git~는 SSH를 복사하기)

-> public key 생성(다른포스팅참고)

git remote add origin git@github.com:깃허브아이디/Repository이름.git


확인

git remote -v 

혹은

git remote

출력:

origin git@github.com:깃허브아이디/Repository이름.git (fetch)

origin git@github.com:깃허브아이디/Repository이름.git (push)


 

commit후 push하기

 

git push -u origin main

 


삭제

git remote rm REMOTE이름

 

 


2) 이미 작업중인 repository를 받아오기

commit내용 push

git push -u origin main

 

문제발생)

push(로컬 git저장소의 commit을 push하고자 할 때): pull은 먼저 시행하라고 한다.

pull: 거절된다.

git pull origin master

-- fatal: refusing to merge unrelated histories

 

이유 -->

github의 repo상태와 로컬에서의 repo상태의 공통 commit지점이 없다.

pull은 fetch와 merge를 해주는 기능이다. 공통 commit지점이 없어서 merge가 안된다.

 

해결방법)

  1) git clone으로 원격저장소를 복제한다.

git clone SSH 혹은 HTTPS 

$ cd 저장소디렉토리
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

$ git log
fatal: your current branch 'master' does not have any commits yet

$ git remote -v
origin  주소 (fetch)
origin  주소 (push)

master 브랜치이다.

원격 저장소 origin이 등록되어있다.

master브랜치 대신 main브랜치를 사용하려면

git branch -M main

 

  2) 강제로 pull 하는 명령어 시행

git pull origin (branchname) --allow-unrelated-histories

 

 

 

 

 

 

참고 포스팅

https://www.lainyzine.com/ko/article/how-to-link-github-remote-repository-and-local-git-repository/

 

GitHub 원격 저장소와 로컬 Git 저장소 연동하는 방법

GitHub은 원격 저장소를 호스팅해주는 서비스로, 본격적인 코드 작업을 하려면 GitHub의 저장소와 로컬 Git 저장소를 연동해야합니다. 이 글에서는 원격 저장소와 로컬 저장소를 연동하는 방법들을

www.lainyzine.com