git-worktree
1.说明git-worktree
git worktree 非常适合大型项目又需要维护多个分支,想要避免来回切换的情况
-
优点
-
git worktree 可以快速进行并行开发,同一个项目多个分支同时并行演进
-
git worktree 的提交可以在同一个项目中共享
-
git worktree 和单独 clone 项目相比,节省了硬盘空间,又因为git worktree使用hard link实现,要远远快于 clone
-
2.新增工作目录add
- 最简单的写法:
git worktree add <新路径>
将基于当前分支,新建一个 worktree 目录,新的分支名就是新建目录的名称。 - 新建一个指定分支
git worktree add <新路径> -b <新分支名>
将基于当前分支,新建一个 worktree 目录,新的分支名是指定的名称。 - 基于指定分支新建一个指定分支
git worktree add <新路径> -b <新分支名> <指定分支名>
将基于指定分支,新建一个 worktree 目录,新的分支名是指定的名称。
2-1.切换出指定分支
git worktree add ../<path_name> <branch>
2-1-1.查看元素工作目录
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
2-1-2.创建工作目录
git worktree add ../build_master master
Preparing worktree (checking out 'master')
HEAD is now at 5d6f19ef xxxx to branch master
2-1-3.查看元素工作目录
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_master 5d6f19ef [master]
2-1-4.切换到build_master
cd ../build_master && git br
* master
+ release-1.3.x
2-1-5.查看branch信息
git branch -vv
* master 5d6f19ef [origin/master] 5d6f19ef xxxx to branch master
+ release-1.3.x 11669902 (/Users/project_local/build) [origin/release-1.3.x: behind 18] xxxx to branch release-1.3.x
2-2.-b/-B使用
git worktree add -b/-B <new_branch> ../<path_name> <branch>
-b
:如果有相同的分支名时,不会生成-B
:如果有相同的分支名时,会把原来的覆盖变为worktree
2-2-1.使用-b切换
如果新建的分支名已经存在的话会报错
2-2-1-1.查看分支
git br
ccc
+ master
* release-1.3.x
2-2-1-2.新建一个已有的分支名
git worktree add -b ccc ../build_ccc release-1.3.x
Preparing worktree (new branch 'ccc')
fatal: A branch named 'ccc' already exists.
2-2-1-3.新建一个不存在的分支名
git worktree add -b ddd ../build_ddd release-1.3.x
Preparing worktree (new branch 'ddd')
HEAD is now at 11669902 xxxxx to branch release-1.3.x
2-2-1-4.查看分支
cd ../build_ddd && git br
ccc
* ddd
+ master
+ release-1.3.x
2-2-2.使用-B切换
如果新建分支名已经存在,会强制改变该分支
2-2-2-1.查看分支
git br
ccc
+ master
* release-1.3.x
2-2-2-2.新建一个已有的分支名
git worktree add -B ccc ../build_ccc master
Preparing worktree (resetting branch 'ccc'; was at 5d6f19ef)
HEAD is now at 5d6f19ef xxxxx to branch master
2-2-2-3.查看分支
git br
+ ccc
+ master
* release-1.3.x
3.删除worktree
3-1.remove
移除一个工作区。只有干净的工作区(没有未跟踪的文件和未修改的跟踪文件)可以被删除。
脏工作区或有子模块的工作区可以用
-f
删除。主工作区不能被删除。
git worktree remove [-f] <path_name>
-f
:强制执行
3-1-1.查看worktree
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_ccc_new 5d6f19ef [ccc]
/Users/project_local/build_ddd 11669902 [ddd]
/Users/project_local/build_master 5d6f19ef [master]
3-1-2.删除某个worktree
git worktree remove build_ddd
3-1-3.查看worktree
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_ccc_new 5d6f19ef [ccc]
/Users/project_local/build_master 5d6f19ef [master]
3-2.prune
修剪
$GIT_DIR/worktrees
中的工作区信息。如果工作目录被手动删除了rm
git worktree prune [-n] [-v] [--expire <time>]
-
-n
:在使用prune时,不会删除任何文件;只会报告它要删除的东西。 -
-v
:使用prune,报告所有的移除情况。 -
--expire <time>
:使用 prune,只对超过<时间>未使用的工作区进行淘汰。
3-2-1.查看文件夹
cd ../ && ls -al
drwxr-xr-x 46 root staff 1472 Jul 13 10:44 build
drwxr-xr-x 50 root staff 1600 Jul 13 11:33 build_ccc_new
drwxr-xr-x 50 root staff 1600 Jul 13 10:47 build_master
3-2-2.删除worktree文件夹
rm -rf build_ccc_new build_master
3-2-3.查看worktree
cd build && git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_ccc_new 5d6f19ef [ccc]
/Users/project_local/build_master 5d6f19ef [master]
3-2-4.使用prune
3-2-4-1.不删除,只查看
git worktree prune -n
Removing worktrees/build_master: gitdir file points to non-existent location
Removing worktrees/build_ccc: gitdir file points to non-existent location
3-2-4-2.删除并输出内容
git worktree prune -v
Removing worktrees/build_master: gitdir file points to non-existent location
Removing worktrees/build_ccc: gitdir file points to non-existent location
3-2-4-3.删除
git worktree prune
3-2-5.查看worktree
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
4.修改move
git worktree move <path_name> <new_path_name>
4-1.案例
4-1-1.查看worktree
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_ccc 5d6f19ef [ccc]
/Users/project_local/build_ddd 11669902 [ddd]
/Users/project_local/build_master 5d6f19ef [master]
4-1-2.修改worktree
git worktree move build_ccc ../build_ccc_new
4-1-3.查看worktree
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_ccc_new 5d6f19ef [ccc]
/Users/project_local/build_ddd 11669902 [ddd]
/Users/project_local/build_master 5d6f19ef [master]
5.查看list
git worktree list [--porcelain]
porcelain
:详细显示
5-1.案列
5-1-2.普通显示
git worktree list
/Users/project_local/build 11669902 [release-1.3.x]
/Users/project_local/build_ccc 5d6f19ef [ccc]
/Users/project_local/build_ddd 11669902 [ddd]
/Users/project_local/build_master 5d6f19ef [master]
5-1-2.porcelain显示
git worktree list --porcelain
worktree /Users/project_local/build
HEAD 116699020d67237a27cafe67ef92128ff4d5d5e8
branch refs/heads/release-1.3.x
worktree /Users/project_local/build_ccc
HEAD 5d6f19efe800908192b9b94ac6362bd81db9daac
branch refs/heads/ccc
worktree /Users/project_local/build_ddd
HEAD 116699020d67237a27cafe67ef92128ff4d5d5e8
branch refs/heads/ddd
worktree /Users/project_local/build_master
HEAD 5d6f19efe800908192b9b94ac6362bd81db9daac
branch refs/heads/master
6.加锁/解锁worktree
6-1.lock
这也可以防止它被移动或删除。 可以选择用
--reason
选项来指定锁定的原因
git worktree lock [--reason <string>] <worktree>
--reason
:说明信息
6-1-1.无提示的锁
git worktree lock <path_name>
6-1-1-1.给build_ccc加锁
git worktree lock build_ccc
6-1-1-2.移除build_ccc
报错信息中没有lock reason
git worktree remove build_ccc
fatal: cannot remove a locked working tree;
use 'remove -f -f' to override or unlock first
6-1-2.增加信息reason
git worktree lock <path_name> <--reason="msg">
6-1-2-1.给build_ccc加锁并添加信息
git worktree lock --reason="test lock" build_ccc
6-1-2-2.移除build_ccc
无法删除,提示报错信息
git worktree remove build_ccc
fatal: cannot remove a locked working tree, lock reason: test lock
use 'remove -f -f' to override or unlock first
6-2.unlock
解锁一个工作区,允许它被修剪、移动或删除。
git worktree unlock <path_name>
# 给build_ccc解锁
git worktree unlock build_ccc