Skill站外收录 @skilllib/skill-2014279427507564546
git-worktrees
支持并行开发与隔离工作流,通过创建独立目录实现多分支同时操作,避免切换干扰。适用于特性开发、代码审查或实验性修改,确保主工作区稳定,任务完成后可安全清理环境。
开发运维 AI 技能
Skills.MD
# Git Worktrees
## Overview
Git worktrees create isolated workspaces sharing the same repository, allowing
work on multiple branches simultaneously without switching. Each worktree is a
separate directory with its own working tree, but they share the same `.git`
history.
## When to Use Worktrees
- **Parallel development**: Work on feature A while feature B builds/tests
- **Code review**: Check out PR branch without disrupting current work
- **Experiments**: Try something risky without affecting main workspace
- **Long-running tasks**: Keep main branch available while feature develops
## Quick Reference
| Action | Command |
|--------|---------|
| List worktrees | `git worktree list` |
| Create worktree | `git worktree add <path> -b <branch>` |
| Create from existing branch | `git worktree add <path> <branch>` |
| Remove worktree | `git worktree remove <path>` |
| Prune stale worktrees | `git worktree prune` |
## Creating Worktrees
### New Feature Branch
```bash
# Create worktree with new branch
git worktree add .worktrees/my-feature -b feat/my-feature
# Or specify base branch
git worktree add .worktrees/my-feature -b feat/my-feature main
```
### From Existing Branch
```bash
# Check out existing remote branch
git worktree add .worktrees/pr-review origin/fix-bug
# Check out existing local branch
git worktree add .worktrees/hotfix hotfix/urgent-fix
```
## Directory Structure
```
project/
├── .git/ # Shared git history
├── .worktrees/ # Convention: keep worktrees here
│ ├── feature-a/ # First worktree
│ └── feature-b/ # Second worktree
└── src/ # Main worktree files
```
## Setup After Creating Worktree
After creating a worktree, you typically need to:
```bash
cd .worktrees/my-feature
# Install dependencies
npm install # or pnpm install, yarn, etc.
# Copy any required env files
cp ../.env .env.local
# Verify setup
npm test
```
## Safety Rules
**NEVER remove a worktree with uncommitted changes without confirmation.**
```bash
# Check for uncommitted changes first
git -C .worktrees/my-feature status --porcelain
# If empty, safe to remove
git worktree remove .worktrees/my-feature
# Delete the branch after merge (-d is safe, fails if not merged)
git branch -d feat/my-feature
```
### Removal Decision Matrix
| PR Merged? | Uncommitted Changes? | Action |
|------------|---------------------|--------|
| Yes | No | Safe to remove |
| Yes | Yes | Ask user - changes will be lost |
| No | No | Do NOT remove - work not preserved |
| No | Yes | Do NOT remove - active work |
## Cleaning Up Worktrees
### Manual Cleanup
```bash
# 1. Check if work is merged (if using GitHub)
gh pr list --head feat/my-feature --state merged
# 2. Check for uncommitted changes
git -C .worktrees/my-feature status --porcelain
# 3. Remove worktree (only if merged or confirmed with user)
git worktree remove .worktrees/my-feature
# 4. Delete branch
git branch -d feat/my-feature
```
### Prune Stale Worktrees
If a worktree directory was deleted manually:
```bash
git worktree prune
```
## Common Patterns
### Review a PR
```bash
# Create worktree from PR branch
git fetch origin pull/123/head:pr-123
git worktree add .worktrees/pr-123 pr-123
# Review, test, then clean up
git worktree remove .worktrees/pr-123
git branch -D pr-123
```
### Parallel Feature Development
```bash
# Main work continues in project root
# Start new feature in worktree
git worktree add .worktrees/new-api -b feat/new-api
# Work on both simultaneously
code .worktrees/new-api # Opens new VS Code window
```
## Troubleshooting
### "Branch already checked out"
A branch can only be checked out in one worktree at a time:
```bash
# Find where branch is checked out
git worktree list
# Remove that worktree first, or use different branch
```
### "Worktree directory not empty"
```bash
# Force add if directory exists but isn't a worktree
git worktree add --force <path> <branch>
```
### Locked Worktree
If a worktree is locked (prevents accidental removal):
```bash
# Unlock it
git worktree unlock <path>
# Then remove
git worktree remove <path>
```
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「git-worktrees」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次