创建并查看 worktree
原片 00:56:02—00:58:02
既然对象不修改,何不再复制一份树的指针,让仓库像复制了一份?这就有了 git worktree。在当前目录 git worktree add,给另一个目录,比如上级下的 hot-fix,建立工作区,应对老板的 hot-fix 任务。
新工作区有 .git,但它与原仓库不同,原来是目录,这个却是文件。

从 git-test 目录创建另一个工作区:
$ git worktree add ../hot-fix
Preparing worktree (new branch 'hot-fix')
HEAD is now at 92255fa Merge branch 'newnew' into new
原 .git 目录包含 objects,保存 persistent data structure。worktree 的 .git 可以 cat,类似 head 引用,指向真实 Git 目录。cd 进入后能看到,它在原仓库 worktrees 下面。
worktrees/hot-fix 中只有指针,没有 object,不必另存对象。有自己的 head,指向合法 HEAD,再指向 commit,就能表示工作状态。

hot-fix 中的 .git 文件,以及它指向的目录里的 HEAD:
$ cat .git
gitdir: /tmp/vibe/git-test/.git/worktrees/hot-fix
# 进入上述目录后
$ cat HEAD
ref: refs/heads/hot-fix
这是持久化对象模型提供的 feature。最初应对多任务,如老板不断打电话,今天则成了 agent 时代的常用特性。