GitGit/GitHub 기초 · 4입문

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

GitGitHubremotepushpullclone

로컬과 원격의 관계

flowchart LR
    subgraph LOCAL["로컬 컴퓨터"]
        L["로컬 저장소"]
    end
    subgraph REMOTE["GitHub"]
        R["원격 저장소\n(origin)"]
    end

    L -->|"git push"| R
    R -->|"git pull"| L
    R -->|"git clone"| L2["새 로컬 저장소"]

GitHub 저장소 만들기

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

로컬을 GitHub에 연결

# 원격 저장소 주소 등록
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: 원격에 업로드

# 현재 브랜치를 push
git push

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

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

pull: 원격에서 가져오기

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

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

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

fetch vs pull

flowchart TB
    REMOTE["원격 저장소"]
    REMOTE -->|"git fetch"| RF["원격 추적 브랜치\norigin/main"]
    RF -->|"git merge"| LOCAL["로컬 브랜치"]
    REMOTE -->|"git pull\n(fetch + merge)"| LOCAL

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


clone: 프로젝트 복제

# 저장소 복제
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 거절

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

원격과 로컬이 갈라진 경우

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 키를 설정합니다.

# 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로 팀 프로젝트에 기여하는 방법을 배웁니다.

궁금한 점이 있으신가요?

협업·의뢰는 아래로, 가벼운 소통은 인스타그램 @bluefox._.hi도 환영이에요.