git-worktree

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

git-worktree - Git Worktree 操作

git-worktree - Git Worktree 操作

複数ブランチを同時に作業するための Git worktree 管理。

使用Git worktree实现多分支并行工作管理。

概要

概要

Git worktree を使うと、1つのリポジトリから複数の作業ディレクトリを作成できる。
メリット:
  • ブランチ切り替えなしで複数機能を並行開発
  • PRレビュー中に別作業が可能
  • 本番ホットフィックスと開発を同時進行

使用Git worktree可以从一个仓库创建多个工作目录。
优点:
  • 无需切换分支即可并行开发多个功能
  • PR评审期间可进行其他工作
  • 可同时进行生产环境热修复与开发工作

基本コマンド

基本命令

Worktree 作成

创建Worktree

bash
undefined
bash
undefined

新規ブランチで作成

基于新分支创建

git worktree add ../project-feature-x feature-x
git worktree add ../project-feature-x feature-x

既存ブランチで作成

基于已有分支创建

git worktree add ../project-hotfix hotfix/urgent-fix
git worktree add ../project-hotfix hotfix/urgent-fix

リモートブランチをチェックアウト

检出远程分支

git worktree add ../project-review origin/feature-y
undefined
git worktree add ../project-review origin/feature-y
undefined

Worktree 一覧

查看Worktree列表

bash
git worktree list
出力例:
/path/to/project          abc1234 [main]
/path/to/project-feature  def5678 [feature-x]
/path/to/project-hotfix   ghi9012 [hotfix/urgent-fix]
bash
git worktree list
输出示例:
/path/to/project          abc1234 [main]
/path/to/project-feature  def5678 [feature-x]
/path/to/project-hotfix   ghi9012 [hotfix/urgent-fix]

Worktree 削除

删除Worktree

bash
undefined
bash
undefined

作業ディレクトリを削除

删除工作目录

rm -rf ../project-feature-x
rm -rf ../project-feature-x

Git から登録解除

从Git中注销

git worktree prune

または一括:
```bash
git worktree remove ../project-feature-x

git worktree prune

或者一键删除:
```bash
git worktree remove ../project-feature-x

ワークフロー例

工作流示例

1. 機能開発中にホットフィックス

1. 功能开发中进行热修复

bash
undefined
bash
undefined

現在: feature-x ブランチで開発中

当前: 在feature-x分支开发中

緊急: 本番バグ発生

紧急情况: 生产环境出现bug

ホットフィックス用 worktree 作成

创建热修复用worktree

git worktree add ../project-hotfix -b hotfix/login-fix main
git worktree add ../project-hotfix -b hotfix/login-fix main

ホットフィックス作業

进行热修复工作

cd ../project-hotfix
cd ../project-hotfix

... 修正 ...

... 修复代码 ...

git commit -m "fix: resolve login issue" git push origin hotfix/login-fix
git commit -m "fix: resolve login issue" git push origin hotfix/login-fix

元の作業に戻る

返回原工作目录

cd ../project
cd ../project

feature-x の作業を継続

继续feature-x的开发

undefined
undefined

2. PRレビュー

2. PR评审

bash
undefined
bash
undefined

レビュー対象のブランチを worktree で開く

用worktree打开待评审分支

git fetch origin git worktree add ../project-review origin/feature-y
git fetch origin git worktree add ../project-review origin/feature-y

レビュー

进行评审

cd ../project-review npm install npm run dev
cd ../project-review npm install npm run dev

レビュー完了後

评审完成后

cd ../project git worktree remove ../project-review

---
cd ../project git worktree remove ../project-review

---

ベストプラクティス

最佳实践

ディレクトリ命名

目录命名规范

project/              # メイン (main)
project-feature-x/    # 機能開発
project-hotfix/       # ホットフィックス
project-review/       # PRレビュー
project/              # 主目录 (main分支)
project-feature-x/    # 功能开发
project-hotfix/       # 热修复
project-review/       # PR评审

定期クリーンアップ

定期清理

bash
undefined
bash
undefined

不要な worktree を確認

查看无用的worktree

git worktree list
git worktree list

マージ済みブランチの worktree を削除

删除已合并分支对应的worktree

git worktree remove ../project-merged-feature
git worktree remove ../project-merged-feature

孤立した worktree を整理

整理孤立的worktree

git worktree prune
undefined
git worktree prune
undefined

注意点

注意事项

  1. 同じブランチを複数 worktree で開けない
  2. node_modules は各 worktree で別途インストール必要
  3. .env ファイルもコピーが必要

  1. 同一分支无法在多个worktree中打开
  2. 每个worktree需单独安装node_modules
  3. .env文件也需要单独复制

トラブルシューティング

故障排除

"already checked out" エラー

"already checked out" 错误

bash
undefined
bash
undefined

別の worktree で使用中のブランチ

分支已在其他worktree中使用

git worktree list # どこで使われているか確認
undefined
git worktree list # 查看该分支的使用位置
undefined

孤立した worktree

孤立的worktree

bash
undefined
bash
undefined

ディレクトリを手動削除した場合

手动删除目录后执行

git worktree prune
undefined
git worktree prune
undefined

ブランチ削除時

删除分支时

bash
undefined
bash
undefined

worktree で使用中のブランチは削除できない

无法删除正在worktree中使用的分支

先に worktree を削除する

需先删除对应的worktree

git worktree remove ../project-feature git branch -d feature
undefined
git worktree remove ../project-feature git branch -d feature
undefined