Git Worktree

git worktree

同一仓库创建多个工作树 例如当前正在 dev 分支下开发, 有很多代码未提交, 同时正运行 server, 突然有一个紧急 bug 需要修复 原来的步骤需要停止 server, git stash, 切换到 fix 分支 现在只需要新建一个新的工作树

创建新的工作树

1
2
3
4
git worktree add <路径> <分支>

# eg: 切换到已有分支
git worktree add ../my-feature-branch feature-branch

移除工作树

1
git worktree remove <路径>

列出所有工作树

1
git worktree list

清理孤立的工作树

1
git worktree prune

查看当前的工作树状态

1
git worktree status

更多参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
git worktree --help
======        
git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]
				  [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]
git worktree list [-v | --porcelain [-z]]
git worktree lock [--reason <string>] <worktree>
git worktree move <worktree> <new-path>
git worktree prune [-n] [-v] [--expire <expire>]
git worktree remove [-f] <worktree>
git worktree repair [<path>...]
git worktree unlock <worktree>
===
build with Hugo, theme Stack, visits 0