什么是GIT_WORK_TREE,为什么我从来不需要设置这个ENV变量,为什么现在呢? [英] What is GIT_WORK_TREE, why have I never needed to set this ENV var, why now?

查看:226
本文介绍了什么是GIT_WORK_TREE,为什么我从来不需要设置这个ENV变量,为什么现在呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu Linux下使用Git来同步和部署我的项目。



我在本地Linux工作机器上有Repo,在我的服务器上有两个Repos,裸露的回购和作为一个已部署的应用程序。



它一直运行良好,但现在我为我的其他网站创建了另一个回购站,现在我收到此错误:

  root @ vserver5:/ var / www / ninethsky#git pull origin master 
fatal:/ usr / lib / git-core / git-pull不能在没有工作树的情况下使用。

所以我必须设置一个GIT_WORKING_TREE ENV-Var,但究竟是什么,在哪里设置它



这是我的repo的.git / config:

  [ core] 
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remoteorigin]
url = / home / git /ninethsky/.git
fetch = + refs / heads / *:refs / remotes / origin / *

还有一个包含 bare = true 的回购和我本地工作机器上的回购协议。

然后,

然后我删除了所有的回购站,但最初的一个,现在我得到了:
$ b $ pre $ root $ vserver5:/ var / www / ninethsky# git init
fatal:不指定GIT_DIR(或--git-dir =<目录>)
root @ vserver5:/ var不允许使用GIT_WORK_TREE(或--work-tree =< directory>) / www / ninethsky#git init --git-dir = / var / www / ninethsky
错误:未知选项`git-dir = / var / www / ninet hsky'

我解决了 git init 问题通过取消设置为空的GIT_WORK_TREE。
GIT_WORK_TREE和GIT_DIR未设置。 git init 再次运行,仍然存在 git add。等问题,当涉及到git动作时克隆回购,它被设置为裸。



谢谢,
Joern。

解决方案

如果你有一个非纯粹的git仓库,它有两部分:git目录和工作树。工作树会检出您的源代码,并可能进行任何更改。 git目录通常称为 .git ,它位于工作树的顶层,它包含项目的所有历史记录,配置设置,指向分支的指针,索引(中转区)等。你的git目录是包含文件和目录的目录,它们看起来像这样:

 分支描述HEAD索引日志ORIG_HEAD refs 
config FETCH_HEAD钩子信息对象packed-refs

虽然我上面描述的是默认的git仓库布局,你可以在文件系统中设置任何目录作为你的git目录和工作树。您可以使用 - 工作树 - git-dir 选项将这些目录从默认值更改为 git 或使用 GIT_DIR GIT_WORK_TREE 环境变量。



您看到的错误来自于第一次检查 git pull 确实 - 它必须从工作树上运行。我假设这是由于你设置了 GIT_DIR GIT_WORK_TREE 环境变量。否则,我最好的猜测是你的 .git 目录不能以某种方式访问​​或损坏。如果列出 /var/www/ninethsky/.git 的内容,它看起来像我上面引用的列表吗?所有这些文件和目录是否是您运行命令的用户可读写的,或者他们的权限是否已更改?




更新:在回答附加信息中的问题时,您更新了您的问题: 因为你仍然有 GIT_WORK_TREE 环境变量集,并且,如错误消息所示,假设git init 如果你指定了工作树,你还必须指定git目录。 第二个变体( git init --git-dir = / var / www / ninethsky )失败,因为 - git-dir 应该出现在 init


然而,在这种情况下,您根本不需要指定工作树, 1 所以我会确保你 unset GIT_WORK_TREE 和 GIT_DIR 环境变量。 / p>

1 也就是说,如果你的 / var / www 位于 .git 目录下,这可能被认为是一个坏主意意外地设置了权限,以便它可以访问网页,所以这可能是一个你希望将git目录保存在其他地方的实例 - 但是,由于这些选项显然已经引起了你的混淆,所以最好保持git部分的简单性,以其他方式拒绝访问 .git 目录。


I'm using Git under Ubuntu Linux to sync and deploy my projects.

I have Repo on my local Linux working machine and two repos on my server, one bare repo and the one as a deployed app.

It always worked fine, but now I created another repo for my other website and now I get this error:

root@vserver5:/var/www/ninethsky# git pull origin master
fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.

So I have to set a GIT_WORKING_TREE ENV-Var, but what is this exactly, where to set it?

This is my repo's .git/config:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = /home/git/ninethsky/.git
        fetch = +refs/heads/*:refs/remotes/origin/*

There is another repo with bare = true and a repo on my local working machine.

Then I removed all the repos, but the initial one, now I get:

root@vserver5:/var/www/ninethsky# git init
fatal: GIT_WORK_TREE (or --work-tree=<directory>) not allowed without specifying GIT_DIR (or --git-dir=<directory>)
root@vserver5:/var/www/ninethsky# git init --git-dir=/var/www/ninethsky
error: unknown option `git-dir=/var/www/ninethsky'

I solved the git init problem by unsetting GIT_WORK_TREE, which was set to blank. GIT_WORK_TREE and GIT_DIR are unset. git init works again, still there is a problem with git add . and so on when it comes to git actions in the cloned repo, which was set to bare.

Thanks, Joern.

解决方案

If you have a non-bare git repository, there are two parts to it: the git directory and the working tree. The working tree has your checked out source code, with any changes you might have made. The git directory is normally called .git, and is in the top level of your working tree - this contains all the history of your project, configuration settings, pointers to branches, the index (staging area) and so on. Your git directory is the one that contains files and directories that look like a bit like this:

branches  description  HEAD   index  logs     ORIG_HEAD    refs
config    FETCH_HEAD   hooks  info   objects  packed-refs

While what I've described above is the default layout of a git repository, you can actually set any directories in the filesystem to be your git directory and working tree. You can change these directories from their defaults either with the --work-tree and --git-dir options to git or by using the GIT_DIR and GIT_WORK_TREE environment variables. Usually, however, you shouldn't need to set these.

The error that you see is from one of the first checks that git pull does - it must be run from a working tree. I assume that this is due to you having set the GIT_DIR or GIT_WORK_TREE environment variables. Otherwise, my best guess is that your .git directory is not accessible or corrupted in some way. If you list the contents of /var/www/ninethsky/.git, does it look like the listing I quoted above? Are all of those files and directories readable and writable by the user you're running the command as, or might they have had their permissions changed?


Update: In answer to the points in the additional information you updated your question with:

  • git init presumably fails because you still have the GIT_WORK_TREE environment variable set, and, as the error message says, if you're specifying the work tree, you also have to specify the git directory.
  • The second variant (git init --git-dir=/var/www/ninethsky) fails because the --git-dir should come before the init.

However, in this situation, you don't need to specify the work tree at all, 1 so I would make sure that you unset the GIT_WORK_TREE and GIT_DIR environment variables.

1 That said, it could be considered a bad idea to keep your .git directory under /var/www in case you accidentally set the permissions such that it is web accessible, so this might be an instance where you want to keep the git directory elsewhere - however, since these options are clearly already causing confusion for you, perhaps it's better to keep the git part simple and deny access to the .git directory with other means.

这篇关于什么是GIT_WORK_TREE,为什么我从来不需要设置这个ENV变量,为什么现在呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆