设置每个裸仓库的工作树

14

正如您下面所看到的,我必须设置一个裸仓库的工作树:

cd barerepo
git status
fatal: This operation must be run in a work tree

git --work-tree=/var/www/mywork/ status
# On branch master
nothing to commit (working directory clean)

如何设置工作树(work-tree),以便我不必每次都指定它?

我尝试使用以下方式修改barerepo/config,但它不起作用。

[core]
    repositoryformatversion = 0
    filemode = true
    bare = true
    worktree = /var/www/mywork
3个回答

17

裸库不应该有工作树,所以 Git 会打印出 "fatal: core.bare and core.worktree do not make sense" 错误信息。因此,您需要在存储库的配置文件中设置 bare = false

user@host:~$ cd barerepo
user@host:~/barerepo$ git config --bool core.bare false
user@host:~/barerepo$ git config --path core.worktree /var/www/mywork

然而,如果barerepo之前不存在,你应该使用以下命令:

git init --separate-git-dir=. /var/www/mywork

这个命令还会在工作目录中创建一个指向 git 目录的 .git 文件:

gitdir: /home/user/barerepo

1
@PineappleUndertheSea:好问题。 :) 你将拥有一个空索引,因此下一次提交将删除当前分支中的所有内容。请参见:https://dev59.com/9Ggv5IYBdhLWcg3wY_02#16582931 - Sz.

4
请注意,以下问题和答案来自2012年,但自Git 2.5以来,即使是光秃秃的存储库,您也可以使用以下方式创建单独的工作树:
$ git worktree add /var/www/mywork/ master
$ git worktree add /var/www/workdev/ devel

请参考git-worktree(1)

它不会改变core.worktree,而是在您的git存储库中创建一个worktrees目录。

如果您不希望所有工作树和裸版本库共享相同的配置,则可以将配置选项extensions.worktreeConfig更改为true。


1
添加工作树与移动主要工作树完全不同。裸仓库可以有这些附属工作树,因为它们只是作为草稿位置,但是定义上不能拥有主要工作树,这就是core.worktree的作用。 - CapinWinky

3
请注意,该提议的解决方案(2012 年,Git 2.5 之前,将在 2015 年 7 月发布)将无法直接与 git config 命令一起使用,会一直出现以下错误:
fatal: core.bare and core.worktree do not make sense.

这就是Git 2.5 (2015年7月)将要解决的问题:

请参见commit fada767 (由Jeff King (peff)于2015年5月29日提交)。
(由Junio C Hamano -- gitster --于2015年6月16日在commit 103b6f9中合并)

setup_git_directory: 延迟 core.bare/core.worktree 错误

如果同时设置了 core.barecore.worktree,我们会抱怨错误的配置并退出。
退出是好的,因为它可以避免命令在可能不正确的设置下运行和造成损坏。
但是,在那里退出是不好的,因为它意味着甚至不关心工作树的命令也无法运行。
这可能会使修复情况更加困难

  [setup]
  $ git config core.bare true
  $ git config core.worktree /some/path

  [OK, expected.]
  $ git status
  fatal: core.bare and core.worktree do not make sense

  [Hrm...]
  $ git config --unset core.worktree
  fatal: core.bare and core.worktree do not make sense

  [Nope...]
  $ git config --edit
  fatal: core.bare and core.worktree do not make sense

  [Gaaah.]
  $ git help config
  fatal: core.bare and core.worktree do not make sense

相反,当我们注意到虚假配置时(即对于所有命令),让我们发出有关虚假配置的警告,但仅在命令尝试使用工作树(通过调用setup_work_tree)时才退出。因此,现在我们得到:
$ git status
warning: core.bare and core.worktree do not make sense
fatal: unable to set up work tree using invalid config

$ git config --unset core.worktree
warning: core.bare and core.worktree do not make sense

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接