using git worktree to work on feature branch easily
Published on April 16, 2025
markdowngist
readme.md
# Git Worktree Cheat Sheet## What is `git worktree`?Allows you to work on multiple branches **in parallel** using **multiple working directories**.---## Setup Instructions### 1. Go to your main repository\`\`\`bashcd ~/projects/your-main-repo\`\`\`### 2. Create and add a worktree for a branch\`\`\`bashgit worktree add ../your-main-repo-feature-a feature-a\`\`\`- This creates a new folder: `../your-main-repo-feature-a`- Checks out `feature-a` branch inside it- Creates the branch if it doesn't already exist### 3. Add another one\`\`\`bashgit worktree add ../your-main-repo-feature-b feature-b\`\`\`Now you have:- `your-main-repo/` (e.g. on main)- `your-main-repo-feature-a/` (on feature-a)- `your-main-repo-feature-b/` (on feature-b)## Usage Tips- Open each folder in a separate terminal or on your IDE window- Work, commit, pull/push like usual in each- No need to switch branches manually## CleanupRemove a worktree:\`\`\`bashgit worktree remove ../your-main-repo-feature-a\`\`\`## Delete the branch (optional):\`\`\`bashgit branch -d feature-a\`\`\`## List active worktrees\`\`\`bashgit worktree list\`\`\`## Full Flow\`\`\`bash# Main repocd ~/projects/my-repo# Create worktree for feature-agit worktree add ../my-repo-feature-a feature-a# Create worktree for feature-bgit worktree add ../my-repo-feature-b feature-b# List all active worktreesgit worktree list\`\`\`