Thing about programming/Git

[Git] 로컬 폴더 - 깃허브 레포 연결

EEOOOO 2022. 7. 15. 11:12

몇 번을 해도 프로젝트 시작할 때마다 번번히 헷갈리는 것 같습니다.

 

정리하고자 작성합니다. 


 

사실 command에서 

> git 

을 작성하고 나오는 명령어들이 거의 핵심이라서 그것들 위주로 익혀만 둬도 기본 기능들을 유용하게 사용할 수 있습니다.

 

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

 

이게 그런 식으로 들어갔을 때 안내하는 명령어들입니다. 

 

위의 start a working area 관련된 명령어들 사용 방식입니다. 

 

 

1. github 페이지에 들어가서 Repository를 새로 만듭니다.

 

2. command 에 들어가 'git clone [HTTPS URL]'을 작성합니다.

 

3. git init으로 초기화시켜주면 로컬 폴더에서 git으로 push, pull 등 모든 기능 연결할 수 있게 됩니다. 

 

 

물론 이전에 git이 로컬 컴퓨터에 설치가 되어 있어야 합니다. 

 


 

만약 깃허브 웹사이트에서 레포지토리를 먼저 만들고 시작하는 것이 아닌 경우에는 바로 연결시키는 방식도 존재합니다.

 

아래 공식사이트에서 그 방법을 찾아 그대로 사용하시면 됩니다. 

 

https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#creating-remote-repositories

 

About remote repositories - GitHub Docs

About remote repositories A remote URL is Git's fancy way of saying "the place where your code is stored." That URL could be your repository on GitHub, or another user's fork, or even on a completely different server. You can only push to two types of URL

docs.github.com

 

 


 

그리고 하나 더, 처음 git을 구동하면 mater branch로 잡혀 있습니다.

하지만 github에서는 몇 년 전 모종의 이유로 디폴트 브랜치 명을 master에서 main으로 변경했습니다.

master-slave라는 단어가 도덕적으로 좋지 않다, master보다 main이 글자 수가 적어 데이터용량상 좋다 등등의 이유로요.)

해서

> git branch -m master main 

으로 현 브랜치를 바꿔준 뒤 push해야 master브랜치를 기본으로 첫 push하는 일이 없게 됩니다.

 

https://stevenmortimer.com/5-steps-to-change-github-default-branch-from-master-to-main/

 

5 steps to change GitHub default branch from master to main

Follow these easy 5 steps to change the default branch name in your repo to 'main' instead of 'master' to show support for removing divisive language in tech.

stevenmortimer.com

 

저는 이걸 놓쳐서 매 번 master로 일단 올리고 github 웹사이트로 들어가 master를 기본 브랜치로 바꿔 main으로 이름을 바꾸고 기존 main을 삭제하고 뭐 그런 식으로 복잡하게 기본 구성을 만들었던 것 같습니다. 

 

라고 하면 되는 줄 알았는데, fast-forward하기 위해 설정해야하는 것이 또 있었습니다.

git-push(1)

의 "Note about fast-forward"를 확인했습니다.

 

그리고 결정적인 문제는 이미 깃허브에 있는 내용을 clone해와서 그 위에 작업했기 때문에 버전이 불일치하는 것이 생겨 거절당하는 것이었습니다. 안전상의 문제로 기존의 내용을 마음대로 못 바꾸게 해놓은 장치입니다.

 

https://backlog.com/git-tutorial/kr/stepup/stepup3_2.html


https://www.digitalocean.com/community/tutorials/how-to-push-an-existing-project-to-github

 

How to Push an Existing Project to GitHub | DigitalOcean

 

www.digitalocean.com