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

worktree: 병렬 checkout

~18 min · worktree, parallel-work

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

worktree는 repo 하나에 checkout을 여러 개 둬

feature/x를 작업하는데 main의 긴급 bug를 고쳐야 하고, 진행 중 코드는 아직 빌드되지 않아 stash하기도 어렵다고 해보자. 옛 해법은 repo를 한 번 더 clone하는 것이었고 현대적인 해법은 git worktree야. 여러 branch의 checkout이 하나의 .git/을 공유해 같은 commit, hook, config를 쓰면서 작업 트리만 나란히 둬.

git worktree add ../repo-hotfix hotfix/login../repo-hotfixhotfix/login을 checkout한 새 worktree를 만들어. 원래 ./repo는 여전히 feature/x에 있어. branch를 계속 switch하는 대신 directory를 오가는 셈이지. .git/을 공유하므로 어느 worktree에서 만든 commit도 다른 쪽에서 곧바로 보여. git worktree list는 checkout을 나열하고 git worktree remove ../repo-hotfix는 끝난 작업 공간을 치워.

쓸모가 큰 경우는 셋이야. 진행 중 feature를 건드리지 않고 hotfix를 고쳐 ship하기, 옛 commit의 worktree에서 긴 test를 돌리는 동안 원래 worktree에서 계속 개발하기, git worktree add ../repo-pr-142 pr/142로 main checkout을 흔들지 않고 동료 PR을 검토하기야.

함정도 둘 있어. 같은 branch를 worktree 둘에 동시에 checkout할 수 없고 Git이 거부해. 읽기 전용 모습만 원한다면 git worktree add --detach를 써. 또 worktree마다 전체 작업 트리가 있어 disk를 쓰지만 별도의 .git/objects/는 없어. 큰 repo에서는 secondary worktree의 partial clone이나 sparse checkout으로 공간을 아낄 수 있어. Vercel 같은 플랫폼에서는 각 worktree를 독립적으로 배포하는 CI 흐름에도 잘 맞아.

Code

흔한 worktree workflow·bash
# 다른 branch 의 새 worktree 추가:
git worktree add ../repo-hotfix hotfix/login

# 특정 commit 에서 worktree (detached HEAD):
git worktree add --detach ../repo-snapshot v1.4.0

# 새 branch 만들면서 worktree 추가:
git worktree add -b feature/profile ../repo-profile main

# 이 repo 의 모든 worktree list:
git worktree list

# 작업 끝난 worktree 은퇴:
git worktree remove ../repo-hotfix

# Worktree 디렉토리 수동 삭제했으면 registry prune:
git worktree prune
Worktree 로 PR 리뷰·bash
# gh 로 PR branch fetch + worktree 생성:
gh pr checkout 142
# 현재 worktree 가 switch 됨. 더 나은 방법:

git fetch origin pull/142/head:pr/142
git worktree add ../repo-pr-142 pr/142
cd ../repo-pr-142
npm install
npm run dev          # 그들 버전 앱 실행
# 리뷰 끝나면:
cd ../repo
git worktree remove ../repo-pr-142
git branch -D pr/142

External links

Exercise

활성 프로젝트에 git worktree add ../repo-side hotfix-test로 두 번째 worktree를 만들고 git worktree list에서 둘을 확인해. 새 worktree에서 commit한 뒤 원래 directory로 돌아와 git log에 그 commit이 보이는지 확인해. git worktree remove로 새 worktree를 치우고, 지금은 stash로 처리하지만 worktree가 더 잘 맞을 작업 하나를 적어봐.

Progress

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

댓글 0

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

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