Work on Multiple Branches Simultaneously with Worktrees
Use Git worktrees to check out multiple branches in separate directories, enabling parallel work without stashing or context switching.
Prerequisites
- -Git 2.5 or later
- -A repository with at least two branches
Steps
Create a worktree for an existing branch
Creates a new directory at ../project-hotfix with the hotfix/urgent-fix branch checked out. Your main working tree is unaffected.
The directory must not already exist. Git will create it for you.
Create a worktree with a new branch
Creates a new branch feature/new-api based on main and checks it out in ../project-api. Combines branch creation and worktree setup.
List all active worktrees
Shows all worktrees, their paths, the checked-out branch, and the HEAD commit for each.
Work in the new worktree
Navigate to the worktree directory and work as you normally would. Each worktree is a fully independent checkout sharing the same .git objects.
You can open each worktree in a separate editor window or terminal tab for true parallel development.
Remove a worktree when finished
Cleans up the worktree directory and unregisters it. The branch itself is not deleted.
If the worktree has uncommitted changes, Git will refuse to remove it. Use --force to override, but you will lose uncommitted work.
Prune stale worktree references
Removes worktree metadata for directories that were manually deleted instead of using 'git worktree remove'.
Full Script
FAQ
Discussion
Loading comments...