본문 바로가기
C.W.K.
Stream
Lesson 01 of 05 · published

git init vs git clone

~16 min · init, clone

Level 0추적 전 새싹
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

처음부터 만들 땐 init, 있던 것을 가져올 땐 clone

디스크에 Git repo를 마련하는 방법은 둘이야. 기존 이력을 함께 가져오는 git clone과 새 이력을 시작하는 git init이지. 둘 다 .git/ 폴더를 만들지만 출발 상태는 전혀 달라. 어느 쪽이 필요한지 구분하는 게 daily loop의 첫걸음이야.

git init은 현재 directory에 빈 repo를 만들어. commit도 없고 첫 commit 전에는 branch도 없으며 remote도 없어. 기본 branch 이름은 init.defaultBranch를 따라가. global 값은 main으로 정해두는 편이 좋아. 옛 기본값인 master는 관리되지 않은 Linux 환경에서 불쑥 튀어나오는 짐이거든. 보통 git init 뒤에 .gitignore를 만들고 git add .한 다음, 첫 commit으로 프로젝트 이력을 시작해.

git clone은 기존 repo를 받아와. 기본적으로 각 branch를 remote-tracking ref(origin/main, origin/feature 등)로 가져오고, 기본 branch를 checkout하며, clone URL을 가리키는 origin remote까지 설정해. SSH URL(git@github.com:user/repo.git)은 날마다 password를 묻지 않게 해줘. HTTPS URL(https://github.com/user/repo.git)은 제한된 회사 network에서도 널리 통하지만 token이나 credential helper가 필요해.

큰 repo를 받을 때는 두 선택지를 알아두면 좋아. --depth 1은 최신 commit만 받는 shallow clone이라 빠르지만 이력이 없어 blamebisect가 제 구실을 못 해. --filter=blob:none은 commit graph와 tree 구조를 먼저 받고 blob은 checkout할 때 늦게 가져오는 partial clone이야. Git 2.20+에서 쓸 수 있고, 대부분의 작업에서는 shallow clone보다 현대적이고 빠른 선택이야.

Code

완전히 새 프로젝트 — init 흐름·bash
mkdir new-thing && cd new-thing
git init
echo '.env' >> .gitignore
echo 'node_modules/' >> .gitignore
git add .gitignore
git commit -m "Add starter .gitignore"
git log --oneline    # commit 하나
기존 프로젝트 — clone 흐름·bash
# SSH (daily dev 에 추천):
git clone git@github.com:org/repo.git

# HTTPS (방화벽 네트워크, CI):
git clone https://github.com/org/repo.git

# 커스텀 폴더 이름:
git clone git@github.com:org/repo.git my-folder

# Shallow (빠름, 전체 history 없음):
git clone --depth 1 https://github.com/org/big-repo.git

# Partial (lazy blob fetch — 대부분 케이스에 shallow 보다 나음):
git clone --filter=blob:none https://github.com/org/big-repo.git

External links

Exercise

아직 Git을 쓰지 않는 작은 side project 폴더를 골라 git init하고, 실제 .gitignore를 작성해 commit해. 다른 머신이나 directory에서는 public repo를 SSH URL로(key가 있다면), 다시 HTTPS URL로 각각 git clone해봐. 두 경우 git remote -v가 무엇을 보여주는지 비교하고 init과 clone을 언제 쓸지 한 줄로 적어.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.