git clone
한 줄 요약
원격 저장소를 로컬에 복제
선행 지식
- Remote란 - 원격 저장소 개념
기본 사용법
git clone <저장소-URL>원격 Repository의 전체 복사본을 로컬에 생성합니다.
주요 옵션
| 옵션 | 설명 | 예시 |
|---|---|---|
<URL> | 저장소 복제 | git clone https://... |
<URL> <폴더명> | 폴더명 지정 | git clone URL my-project |
--depth 1 | 얕은 복제 | git clone --depth 1 URL |
-b <branch> | 특정 브랜치 | git clone -b develop URL |
--single-branch | 단일 브랜치만 | git clone --single-branch URL |
실제 예제
기본 복제
$ git clone https://github.com/user/awesome-project.git
Cloning into 'awesome-project'...
remote: Enumerating objects: 150, done.
remote: Counting objects: 100% (150/150), done.
remote: Compressing objects: 100% (100/100), done.
Receiving objects: 100% (150/150), 50.00 KiB | 2.00 MiB/s, done.
Resolving deltas: 100% (50/50), done.
$ cd awesome-project
$ ls
README.md src/ package.json폴더명 지정
# my-project라는 이름으로 복제
git clone https://github.com/user/repo.git my-project
cd my-project현재 폴더에 복제
# 빈 폴더에서 실행
git clone https://github.com/user/repo.git .특정 브랜치 복제
# develop 브랜치만 복제
git clone -b develop https://github.com/user/repo.git얕은 복제 (shallow clone)
전체 히스토리가 필요 없을 때 빠르게 복제:
# 최신 커밋만 복제
git clone --depth 1 https://github.com/user/repo.git얕은 복제 용도
- CI/CD에서 빠른 체크아웃
- 대용량 저장소에서 빠른 시작
- 히스토리 불필요할 때
clone이 설정하는 것들
clone을 실행하면 자동으로:
- 폴더 생성: 저장소 이름으로 폴더 생성
- Remote 설정:
origin으로 원격 저장소 연결 - 모든 브랜치: 원격 브랜치 정보 다운로드
- 기본 브랜치 체크아웃: main (또는 master) 브랜치 활성화
# clone 후 확인
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
$ git branch -a
* main
remotes/origin/main
remotes/origin/developclone vs init
| git clone | git init |
|---|---|
| 기존 저장소 복제 | 새 저장소 생성 |
| 히스토리 포함 | 빈 저장소 |
| Remote 자동 설정 | Remote 수동 설정 필요 |
# 기존 프로젝트 가져올 때
git clone https://github.com/user/repo.git
# 새 프로젝트 시작할 때
mkdir new-project
cd new-project
git init인증 방식
HTTPS
git clone https://github.com/user/repo.git
# 사용자명/비밀번호 또는 토큰 입력 요청SSH
git clone git@github.com:user/repo.git
# SSH 키로 인증 (설정 필요)→ HTTPS vs SSH 참고
자주 하는 실수
폴더가 이미 존재
$ git clone https://github.com/user/repo.git
fatal: destination path 'repo' already exists and is not an empty directory.해결:
# 다른 폴더명 지정
git clone https://github.com/user/repo.git repo2
# 또는 기존 폴더 삭제/이동 후 clone권한 없는 private 저장소
$ git clone https://github.com/user/private-repo.git
fatal: repository 'https://...' not found해결:
- 저장소 접근 권한 확인
- 인증 정보 확인 (토큰, SSH 키)
- 저장소 URL 재확인
clone 후 push 권한 없음
$ git push
remote: Permission denied.원인: 다른 사람 저장소를 clone 함
해결:
- Fork 후 내 저장소 clone
- 또는 Collaborator로 추가 요청
clone 후 작업 흐름
# 1. 저장소 복제
git clone https://github.com/user/repo.git
cd repo
# 2. 새 브랜치에서 작업
git switch -c feature/my-feature
# 3. 작업 및 커밋
git add .
git commit -m "feat: 새 기능 추가"
# 4. 원격에 push (Fork한 경우)
git push origin feature/my-feature
# 5. Pull Request 생성 (GitHub에서)관련 명령어
- git init - 새 저장소 생성
- git remote - Remote 관리
- git pull과 fetch - 변경사항 가져오기
더 알아보기
- Remote란 - 원격 저장소 개념
- Repository - 저장소 구조
- Pull Request란 - Fork 후 기여하기