GitHub Copilot 앱 글에서 에이전트마다 워크트리를 하나씩 준다는 설명을 읽었습니다. 워크트리를 손으로 써 본 적이 별로 없어서 공식 문서를 다시 읽었습니다.
한 줄 정의
문서의 첫 문장이 정의 그 자체입니다. git 저장소는 여러 개의 작업 트리를 가질 수 있고, 덕분에 한 번에 여러 브랜치를 꺼내 둘 수 있습니다.
보통은 브랜치를 바꾸려면 git switch로 작업 폴더 전체를 바꿉니다. 워크트리는 브랜치마다 폴더를 따로 둡니다.
메인과 연결 워크트리
- 메인 워크트리:
git clone이나 git init으로 만든 원래 폴더. 하나뿐이고 지울 수 없음
- 연결 워크트리:
git worktree add로 추가한 폴더. 0개 이상
자주 쓰는 명령
# 새 브랜치 emergency-fix를 만들어 ../temp에 꺼낸다
git worktree add -b emergency-fix ../temp master
# 경로 이름으로 브랜치를 만든다(../hotfix → hotfix 브랜치)
git worktree add ../hotfix
# 목록: 메인이 먼저, 그다음 연결 워크트리
git worktree list
# 정리: 변경 없는 워크트리만 지울 수 있다(아니면 --force)
git worktree remove ../temp
# 폴더를 손으로 지웠다면 남은 관리 정보를 정리
git worktree prune
같은 브랜치는 한 곳에서만
헷갈리기 쉬운 제약이 하나 있습니다. 다른 워크트리가 이미 꺼낸 브랜치로는 add가 거부됩니다. 같은 브랜치를 두 폴더에서 동시에 고치면 꼬이기 때문입니다.
우회하려면 새 브랜치를 만들거나(-b), 브랜치 없이 커밋만 꺼내면(--detach) 됩니다. 에이전트 도구들이 세션마다 새 브랜치를 만드는 이유가 여기 있습니다.
무엇이 공유되나
| 공유 |
워크트리마다 따로 |
커밋·객체, refs/로 시작하는 참조(브랜치·태그), 설정(기본값) |
HEAD, 인덱스(스테이징) |
저장소를 통째로 복사하는 git clone과 다른 점이 이것입니다. 객체를 공유하니 디스크를 적게 쓰고, 한 워크트리에서 만든 커밋이 다른 워크트리에서도 바로 보입니다.
에이전트와 쓸 때
에이전트 셋을 동시에 돌린다면 이렇게 하면 됩니다.
- 작업마다
git worktree add -b agent/작업명 ../wt-작업명
- 에이전트를 각 폴더에서 실행
- 끝나면 PR로 올리고
git worktree remove
도구가 자동으로 해 주더라도 원리를 알아 두면 꼬였을 때 풀 수 있습니다. 폴더를 그냥 지웠다면 prune을 잊지 마세요.
I read in the GitHub Copilot app article that each agent gets its own worktree. I haven't used worktrees much by hand, so I re-read the official docs.
One-line definition
The first sentence of the docs is the definition itself. A git repository can have multiple working trees, which lets you check out multiple branches at once.
Normally, to switch branches you change the entire working folder with git switch. Worktrees keep a separate folder per branch.
Main and linked worktrees
- Main worktree: The original folder created by
git clone or git init. There is only one and it cannot be removed.
- Linked worktree: A folder added with
git worktree add. There can be zero or more.
Commonly used commands
# 새 브랜치 emergency-fix를 만들어 ../temp에 꺼낸다
git worktree add -b emergency-fix ../temp master
# 경로 이름으로 브랜치를 만든다(../hotfix → hotfix 브랜치)
git worktree add ../hotfix
# 목록: 메인이 먼저, 그다음 연결 워크트리
git worktree list
# 정리: 변경 없는 워크트리만 지울 수 있다(아니면 --force)
git worktree remove ../temp
# 폴더를 손으로 지웠다면 남은 관리 정보를 정리
git worktree prune
A branch can only be checked out in one place
There is one constraint that's easy to get confused about. add is rejected if another worktree has already checked out that branch. This is because modifying the same branch in two folders at the same time causes conflicts.
To work around this, create a new branch (-b) or check out a commit without a branch (--detach). This is why agent tools create a new branch per session.
What is shared
| Shared |
Separate per worktree |
Commits and objects, references starting with refs/ (branches, tags), configuration (defaults) |
HEAD, index (staging) |
This is what differs from git clone, which copies the entire repository. Because objects are shared, it uses less disk space, and commits made in one worktree are immediately visible in other worktrees.
When using with agents
If you're running a set of agents concurrently, do this:
- For each task, run
git worktree add -b agent/task-name ../wt-task-name
- Run the agent in each folder
- When done, open a PR and run
git worktree remove
Even if the tool does this automatically, knowing the underlying mechanics helps you untangle things when they get messy. If you just delete a folder, don't forget to run prune.