简体   繁体   English

.git 本地存储库是否必须包含在本地工作目录中?

[英]Does the .git local repository have to be contained in the local working directory?

Specifically I'm using Eclipse and egit:具体来说,我正在使用 Eclipse 和 egit:

  • using the wizard to share a project with egit, it moved the working directory out of the eclipse workspace into the folder I had chosen to hold git depositories使用向导与 egit 共享项目,它将工作目录从 eclipse 工作区移到我选择保存 git 存储库的文件夹中
  • the .git repository was contained in this folder .git 存储库包含在此文件夹中

My questions are:我的问题是:

  • I read that the git repository should not be in the Eclipse workspace but why can't the working directory be in the Eclipse workspace?我读到 git 存储库不应该在 Eclipse 工作区中,但为什么工作目录不能在 Eclipse 工作区中?
  • Is it because the local repository have to be contained inside the git working directory?是因为本地存储库必须包含在 git 工作目录中吗?

I've found a few questions about moving the directory back, but I don't want to do that without knowing why it was moved in the first place (I don't want to create problems for later).我发现了一些关于将目录移回的问题,但我不想在不知道为什么首先移动目录的情况下这样做(我不想为以后制造问题)。

No, the .git directory does not have to be in your working directory.不, .git目录不必在您的工作目录中。 You can point a Git repository at another directory as its working directory using the --work-tree option :您可以使用--work-tree选项将 Git 存储库指向另一个目录作为其工作目录

--work-tree=<path> --work-tree=<路径>

Set the path to the working tree.设置工作树的路径。 It can be an absolute path or a path relative to the current working directory.它可以是绝对路径,也可以是相对于当前工作目录的路径。 This can also be controlled by setting the GIT_WORK_TREE environment variable and the core.worktree configuration variable (see core.worktree in git-config(1) for a more detailed discussion).这也可以通过设置 GIT_WORK_TREE 环境变量和 core.worktree 配置变量来控制(有关更详细的讨论,请参阅 git-config(1) 中的 core.worktree)。

Consider the following example:考虑以下示例:

$ # Create a Git repository
$ mkdir project
$ cd project
$ git init
Initialized empty Git repository in /path/to/project/.git/
$ ls -a
. .. .git

$ # Now, start working in another directory
$ cd ..
$ mkdir working-directory
$ cd working-directory
$ echo foo > foo.txt

$ # And now, commit to the initially created repository
$ cd ../project
$ git --work-tree=../working-directory status
$ git --work-tree=../working-directory add foo.txt
$ git --work-tree=../working-directory commi

It is very likely that Eclipse is using this option internally. Eclipse 很可能在内部使用此选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM