git 设置每个裸仓库的工作树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11856690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:18:00  来源:igfitidea点击:

setting the work-tree of each bare repo

git

提问by Jürgen Paul

As you can see below I have to set the work-tree of a bare repo:

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

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)

How do I set the work-tree for that repo so that I don't have to specify it everytime?

如何为该 repo 设置工作树,以便我不必每次都指定它?

I tried modifying barerepo/configwith this but it doesn't work.

我尝试barerepo/config用它进行修改,但它不起作用。

[core]
    repositoryformatversion = 0
    filemode = true
    bare = true
    worktree = /var/www/mywork

回答by PleaseStand

Bare repos are not supposed to have a work tree, so git prints the "fatal: core.bare and core.worktree do not make sense" error message. Therefore, you need to set bare = falsein the repo's config file.

裸仓库不应该有工作树,因此 git 打印“致命:core.bare 和 core.worktree 没有意义”错误消息。因此,您需要bare = false在 repo 的配置文件中进行设置。

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

However, if barerepo did not previously exist, you should use this command instead:

但是,如果 barrepo 以前不存在,则应改用此命令:

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

This command will also create a .gitfile in the work tree pointing to the git dir:

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

gitdir: /home/user/barerepo

回答by VonC

Note that the proposed solution (2012, pre Git 2.5 which will be released in July 2015) would notwork directly with git configcommand.
It would keep dying with a:

请注意,提议的解决方案(2012 年,2015 年 7 月发布的 Git 2.5 之前版本)不能直接与git config命令一起使用。
它将继续死亡:

fatal: core.bare and core.worktree do not make sense.

That is what Git 2.5 (July 2015) will address:

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

See commit fada767(29 May 2015) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster--in commit 103b6f9, 16 Jun 2015)

请参阅Jeff King ( ) 的commit fada767(2015 年 5 月 29 日(由Junio C Hamano合并-- --commit 103b6f9,2015 年 6 月 16 日)peff
gitster

setup_git_directory: delay core.bare/core.worktreeerrors

If both core.bareand core.worktreeare set, we complain about the bogus config and die.
Dying is good, because it avoids commands running and doing damage in a potentially incorrect setup.
But dying thereis bad, because it means that commands which do not even care about the work tree cannot run.
This can make repairing the situation harder:

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

Instead, let's issue a warning about the bogus config when we notice it (i.e., for all commands), but only die when the command tries to use the work tree (by calling setup_work_tree).

So we now get:

相反,让我们在注意到虚假配置时发出警告(即,对于所有命令),但只有在命令尝试使用工作树(通过调用 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

回答by marcz

Note that the question and answers are from year 2012, but since git 2.5, even with a bare repository you can create separate worktrees with:

请注意,问题和答案来自 2012 年,但从 git 2.5 开始,即使使用裸存储库,您也可以使用以下命令创建单独的工作树:

$ git worktree add /var/www/mywork/ master
$ git worktree add /var/www/workdev/ devel

See git-worktree(1).

参见git-worktree(1)

It will not change core.worktreebut create a worktreesdirectory in your git repository.

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

You may want to change the configuration option extensions.worktreeConfigto true if you don't want all the worktrees and the bare repository to share the same config.

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