Git Worktree

1 min

Índice

  1. O que é Worktree?
  2. Por que Worktree?
  3. Utilização prática
  4. Comandos essenciais

1. O que é Worktree?

Git worktree = ter várias pastas de trabalho para o MESMO repo.

Conceito:

  • 1 repo Git = normalmente 1 pasta
  • Com worktree = 1 repo, várias pastas
  • Cada pasta pode estar numa branch diferente

Analogia: é como ter vários escritórios para o mesmo projeto.

Voltar ao índice

2. Por que Worktree?

Problema clássico:

bash
# Trabalha em feature-A
git checkout feature-A
# Código, código, código... (trabalho por acabar)

# URGÊNCIA: bug em main!
git stash                # Salvaguardar work in progress
git checkout main        # Mudar para main  
# Corrigir o bug...

git checkout feature-A   # Voltar ao seu trabalho
git stash pop           # Recuperar o work in progress

Solução worktree:

bash
# Trabalha em feature-A na pasta principal
# Urgência? Criar uma nova pasta para main!
git worktree add ../hotfix main

# Agora:
# - ./           → feature-A (o seu trabalho continua)
# - ../hotfix    → main (para a correção urgente)

Vantagem: sem stash/switch. Só 2 pastas, 2 tarefas em paralelo!

Voltar ao índice

3. Utilização prática

Setup de base:

bash
mkdir demo-worktree && cd demo-worktree
git init

echo "print('main v1')" > app.py
git add . && git commit -m "Main version"

# Criar branch feature
git checkout -b feature-new
echo "print('feature work')" > feature.py
git add . && git commit -m "Feature work"

git checkout main

Criar worktrees:

bash
# Criar worktree para feature-new
git worktree add ../feature-work feature-new

# Criar worktree para uma nova branch
git worktree add ../hotfix-branch -b hotfix

# Ver todos os worktrees
git worktree list

Resultado:

demo-worktree/       → main branch
../feature-work/     → feature-new branch  
../hotfix-branch/    → hotfix branch (nova)

Trabalhar em paralelo:

bash
# Terminal 1: pasta principal (main)
cd demo-worktree
echo "print('main updated')" >> app.py
git add . && git commit -m "Update main"

# Terminal 2: Feature work
cd ../feature-work  
echo "print('feature complete')" >> feature.py
git add . && git commit -m "Complete feature"

# Terminal 3: Hotfix
cd ../hotfix-branch
echo "print('urgent fix')" > fix.py
git add . && git commit -m "Critical fix"

Magia: 3 branches, 3 pastas, 0 mudança de contexto!

Voltar ao índice

4. Comandos essenciais

ComandoAção
git worktree add <path> <branch>Criar worktree numa branch existente
git worktree add <path> -b <new-branch>Criar worktree + nova branch
git worktree listVer todos os worktrees
git worktree remove <path>Apagar worktree
git worktree pruneLimpar worktrees apagados

Casos de uso típicos:

1. Hotfix urgente:

bash
git worktree add ../hotfix main
cd ../hotfix && echo "fix" > fix.py
git add . && git commit -m "Hotfix"
git push origin main

2. Review de PR:

bash
git worktree add ../pr-review pr-branch
cd ../pr-review  # Testar a PR sem perturbar o seu trabalho

3. Comparação de branches:

bash
git worktree add ../version-a branch-a
git worktree add ../version-b branch-b
# Compare ficheiros entre ../version-a e ../version-b

Limpeza:

bash
git worktree remove ../feature-work
git worktree remove ../hotfix-branch  
git worktree prune                    # Limpar as referências

Worktree = multitarefa Git sem compromissos!

Voltar ao índice