关于git WorkTree的使用

关于git WorkTree的使用

二月 28, 2022

因为最近开发新的功能模块需要来回切换分支,所以找了一下有没有不需要频繁切换分支的方式来处理这种情况,然后就在官方文档中找到了workTree

git版本需要大于2.6.7

使用方法

1
2
3
4
5
6
7
8
git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]] [-b <new-branch>] <path> [<commit-ish>]
git worktree list [-v | --porcelain]
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>

最常用

1
2
3
git worktree add branchName path #把branchName分支checkout到path路径上
git worktree remove path #移除path路径上的worktree
git worktree list #列出已经存在的所有worktree

作用

这样的话你就能在一次clone的情况下把2个不同的分支放在2个不同的路径上方便进行操作。

不过有一点需要注意的是被workTree释放到其他路径的分支不能在其他的worktree或者是主工作区checkout了