GitHub 원격 저장소
로컬 Git 저장소를 GitHub에 연결하고, push·pull·clone·fetch를 사용해 원격과 동기화합니다.
- GitHub에 원격 저장소를 생성하고 연결할 수 있다
git push로 로컬 코드를 GitHub에 올릴 수 있다git pull로 원격 변경사항을 받을 수 있다git clone으로 기존 저장소를 복사할 수 있다- push/pull/fetch/clone의 차이를 이해한다
1. 로컬 vs 원격 저장소
인터넷 없어도 OK
팀원과 공유
2. 원격 저장소 연결
# 원격 저장소 연결 (origin이라는 이름으로)
$ git remote add origin https://github.com/사용자명/저장소명.git
# 연결 확인
$ git remote -v
origin https://github.com/사용자명/저장소명.git (fetch)
origin https://github.com/사용자명/저장소명.git (push)
origin은 원격 저장소의 별명(nickname)입니다. 관례적으로 기본 원격 저장소를 origin이라 부릅니다.
3. push / pull / fetch / clone
git push — 업로드
# 처음 push (upstream 설정)
$ git push -u origin main
Branch 'main' set up to track remote branch 'main' from 'origin'.
To https://github.com/사용자명/저장소명.git
* [new branch] main -> main
# 이후부터는 간단하게
$ git push
git pull — 다운로드 + 병합
# 원격 변경사항 가져와서 자동 merge
$ git pull origin main
# upstream 설정 후에는
$ git pull
git clone — 저장소 복사
# 원격 저장소를 통째로 복사 (원격 연결 자동 설정됨)
$ git clone https://github.com/사용자명/저장소명.git
Cloning into 'repo-name'...
remote: Enumerating objects: 10, done.
...
Resolving deltas: done.
# 폴더 이름 지정해서 clone
$ git clone https://github.com/사용자명/저장소명.git my-project
| 명령어 | 용도 | 전제 |
|---|---|---|
git clone |
저장소 최초 복사 | 원격 저장소가 이미 존재 |
git push |
로컬 → 원격 업로드 | 원격 연결 완료 |
git pull |
원격 → 로컬 (자동 병합) | 원격 연결 완료 |
git fetch |
원격 → 로컬 (수동 병합) | 원격 연결 완료 |
4. 실습 — 첫 push 완성
$ git remote add origin https://github.com/사용자명/my-first-git.git
$ git branch -M main
$ git push -u origin main
https://github.com/사용자명/my-first-git에 접속, 파일이
업로드됐는지 확인!git remote add origin URL
git remote -v
git push -u origin main
git pull
git fetch
git clone URL
이론의 로컬 ↔ GitHub 다이어그램을 따라, 실제 GitHub 저장소를 만들고 push → pull 전체 흐름을 직접 해보세요.
이론의 STP 1처럼 실제 GitHub를 열고 그대로 따라하세요. README 없이 생성해야 충돌이 없습니다.
GitHub.com → New repository → 이름 입력 → README 체크 해제 → Create repository
이론의 "2단계: origin이라는 이름으로 연결". git remote -v로
연결 확인합니다.
$ git remote add origin https://github.com/사용자명/my-first-git.git
$ git remote -v
origin https://github.com/.../my-first-git.git (fetch)
origin https://github.com/.../my-first-git.git (push) ← 연결 성공!
이론의 "git push 화살표"! -u는 upstream 설정으로 이후부터는
git push만 해도 됩니다.
$ git push -u origin main
Branch 'main' set up to track remote branch 'main' from 'origin'.
To https://github.com/.../my-first-git.git
* [new branch] main -> main ← GitHub에 나타남!
# 이후 커밋하면
$ echo "수정" >> index.html
$ git add . && git commit -m "fix: 내용 수정"
$ git push # -u origin main 없이도 됨!
팀원이 수정한 상황 시뮬레이션! GitHub에서 직접 파일을 수정하고
git pull로 내려받습니다.
# GitHub 웹사이트에서 README.md 직접 편집 후
# 로컬에서 pull
$ git pull
remote: Enumerating objects: 5, done.
...
Updating a1b2c3..d4e5f6
Fast-forward
README.md | 2 ++
1 file changed ← 내려왔어요!
이론의 "git clone". GitHub URL로 저장소를 통째로 복사하면 remote add 없이 자동으로 연결됩니다.
# 다른 위치에 clone
$ git clone https://github.com/사용자명/my-first-git.git cloned-project
Cloning into 'cloned-project'...
remote: Enumerating objects...
Done. ← 자동으로 origin 연결!
$ cd cloned-project
$ git remote -v
origin https://github.com/.../my-first-git.git (fetch)
원격에 로컬에 없는 커밋이 있기 때문입니다.
먼저 git pull로 원격 커밋을 받은 후 다시 push하세요. 충돌 났다면 해결
후 push합니다.
git push 했는데 인증요청이 나와요GitHub 비밀번호 방식은 이제 지원하지 않습니다.
GitHub 설정 → Developer Settings → Personal Access Token을 생성해 비밀번호 자리에 입력하세요. 또는 SSH 키를 설정하세요.
로컬와 원격이 같은 파일을 다르게 수정한 경우입니다.
Ch.06의 충돌 해결과 동일합니다. 충돌 파일을 수정하고 add + commit하면 됩니다.