Worktrees: Git's best kept secret (and why you should use them)
16 days ago
- #worktree
- #git
- #workflow
- Git worktrees allow multiple working directories (worktrees) from a single repository, each pointing to different branches.
- Worktrees enable switching between branches by changing directories, eliminating the need for stashing or committing changes before switching.
- A typical workflow with worktrees simplifies handling tasks like hotfixes, code reviews, maintaining different versions, and managing long-running tests.
- Basic worktree commands include `git worktree add`, `git worktree remove`, and `git worktree list`.
- Avoid creating worktrees inside another worktree to prevent repository mess.
- Two recommended setups for worktrees: using a project folder for organization or a bare repository for more flexibility.
- Caveats include a branch being limited to one worktree, duplicated search results from parent folders, and untracked files not being copied.
- Helper scripts and Visual Studio Code extensions like Gitlens and Git Worktree Menu enhance the worktree experience.