Worktree Commands
git worktreelist- Show all active worktrees.add <path> <branch_name>- Create a worktree for an existing branch.add <path> <commit_hash>- Create a worktree at a specific commit in adetached HEADstate.remove <path>- Remove the worktree folder and its reference.
How?
- A worktree is simply another folder connected to the same repository.
- It can point to any branch or commit.
- Each worktree is isolated in its working files, but all of them share the same Git history.
- Switching to a worktree is simply moving into that folder with
cd.
Why?
- You can work on different branches or commits in parallel.
- You avoid stashing or making temporary commits just to move around.
- Each task stays in its own separate folder.
Worktree Strategies
With this warning in mind, there are two approaches to structuring projects when working with worktrees:1. Keep worktrees outside the main working directory
E.g., right next to your main project folder.2. Use a bare clone
git clone --bare <remote_url>: Create a local bare copy of a remote repository.
A bare clone is a repository without a working directory. It contains only the
.git data.To start working, you need to create a worktree.