GitGit/GitHub 기초강의 6개 중 4번째난이도 입문

GitHub 원격 저장소 — push, pull, clone

발행 읽는 시간 약 1 분

  • Git
  • GitHub
  • remote
  • push
  • pull
  • clone

강의 소개

로컬 저장소를 GitHub에 연결하고, push로 업로드하고, pull로 최신 상태를 가져오는 방법을 배웁니다. clone으로 프로젝트를 복제하는 것도 익힙니다.

로컬과 원격의 관계#

Diagram · Mermaid

GitHub 저장소 만들기#

  1. github.com에서 New repository 클릭
  2. Repository name 입력
  3. Public / Private 선택
  4. Create repository 클릭

로컬을 GitHub에 연결#

Bash
# 원격 저장소 주소 등록
git remote add origin https://github.com/username/my-project.git

# 등록된 원격 확인
git remote -v
# origin  https://github.com/username/my-project.git (fetch)
# origin  https://github.com/username/my-project.git (push)

# 첫 push (-u는 upstream 설정, 이후 git push만 하면 됨)
git push -u origin main

push: 원격에 업로드#

Bash
# 현재 브랜치를 push
git push

# 특정 브랜치 push
git push origin feature/login

# 새 브랜치를 원격에 처음 push
git push -u origin feature/login

pull: 원격에서 가져오기#

Bash
# 원격 변경사항 가져와서 병합
git pull

# 특정 브랜치에서 pull
git pull origin main

# fetch + merge를 분리해서 실행
git fetch origin        # 원격 변경사항 다운로드 (병합 안 함)
git merge origin/main   # 병합

fetch vs pull#

Diagram · Mermaid

fetch를 먼저 해서 변경사항을 확인한 후 merge하는 것이 안전합니다.


clone: 프로젝트 복제#

Bash
# 저장소 복제
git clone https://github.com/username/my-project.git

# 폴더 이름 지정
git clone https://github.com/username/my-project.git my-folder

# 특정 브랜치만 clone
git clone -b develop https://github.com/username/my-project.git

자주 보는 상황들#

충돌 없이 pull 거절#

Bash
git pull
# error: Your local changes to the following files would be overwritten by merge

# 해결법 1: 변경 임시 저장
git stash
git pull
git stash pop

# 해결법 2: 로컬 변경 커밋 후 pull
git add .
git commit -m "로컬 변경 저장"
git pull

원격과 로컬이 갈라진 경우#

Bash
git pull
# hint: You have divergent branches and need to specify how to reconcile them.

# rebase로 깔끔하게 통합
git pull --rebase origin main

SSH 키로 인증 (권장)#

매번 비밀번호 입력 없이 push/pull하려면 SSH 키를 설정합니다.

Bash
# SSH 키 생성
ssh-keygen -t ed25519 -C "hong@example.com"

# 공개키 확인 (GitHub에 등록)
cat ~/.ssh/id_ed25519.pub

# 연결 테스트
ssh -T git@github.com
# Hi username! You've successfully authenticated.

# 원격 주소를 SSH로 변경
git remote set-url origin git@github.com:username/my-project.git

정리#

명령어역할
git remote add origin <url>원격 저장소 등록
git push -u origin main첫 push + upstream 설정
git push현재 브랜치 push
git pull원격 변경사항 가져와 병합
git fetch원격 변경사항만 다운로드
git clone <url>저장소 복제

다음 편에서는 협업 워크플로우 — Pull Request와 Fork로 팀 프로젝트에 기여하는 방법을 배웁니다.

Follow · Ask · Work

이 글이 도움이 됐다면

짧은 작업 기록과 데모는 인스타그램에 올립니다. 만들고 싶은 것이 있다면 프로젝트 의뢰나 협업 제안도 편하게 보내 주세요.

글쓴이

파란여우

직접 만든 AI 앱·제품의 개발·배포를 공개하는 실전형 테크 크리에이터

파란여우 소개 Instagram @bluefox._.hi

이어서 보기

만들고 싶은 것이 있다면 프로젝트 의뢰로 알려 주세요.