What is GIT_WORK_TREE, why have I never had to install this ENV var, why now?

I use Git under Ubuntu Linux to synchronize and deploy my projects.

I have a Repo on my local Linux workstation and two repositories on my server, one bare repo and one as a deployed application.

It always worked fine, but now I created another repo for my other site, 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 GIT_WORKING_TREE ENV-Var, but what exactly is it, where to install it?

This is my repo .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 deleted all the repositories, but the original 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 disabling GIT_WORK_TREE, which was set as empty. GIT_WORK_TREE and GIT_DIR are not set. git init works again, there is still a problem with git add . etc. when it comes to git actions in a cloned repo that was set to bare.

Thanks, Jorn.

+46
git
Mar 12 2018-11-11T00:
source share
2 answers

If you have a non-bare git repository, there are two parts for it: the git directory and the working tree. In the working tree, you can check the source code with any changes you could make. The git directory is usually called .git and is located at the top level of your working tree - this contains the entire history of your project, configuration settings, pointers to branches, index (setting area), etc. Your git directory is the one that contains files and directories that look something like this:

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

While what I described above is the default layout in the git repository, you can actually set any directories in the file system as your git directory and working tree. You can change these directories from their defaults either with the --work-tree and --git-dir to git , or using the GIT_DIR and GIT_WORK_TREE environment variables. Usually, however, they do not need to be installed.

The error you see is one of the first checks performed by git pull - it should start from a working tree. I assume this is because you set the environment variables GIT_DIR or GIT_WORK_TREE . Otherwise, it is best to assume that your .git directory is inaccessible or corrupted in some way. If you list the contents of /var/www/ninethsky/.git , does this look like the list I specified above? Are all files and directories read and written by the user to whom you are executing the command, or could they have their permissions?




Update: In response to the points of additional information, you updated your question:

  • git init supposedly fails because you still have the GIT_WORK_TREE environment GIT_WORK_TREE , and as the error message says, if you specify the work tree, you also need to specify the git directory.
  • The second option ( git init --git-dir=/var/www/ninethsky ) fails because --git-dir should appear before init .

However, in this situation you do not need to specify the work tree at all, 1 so I would make sure that you disable the GIT_WORK_TREE and GIT_DIR environment variables.

1 This may be considered a bad idea to keep your .git directory under /var/www in case you accidentally set permissions so that it is accessible over the Internet, so this may be the instance where you want to save the git directory to another location, however since these options are clearly already creating confusion for you, it might be better to keep the git part simple and block access to the .git directory in other ways.

+95
Mar 12 '11 at 15:35
source share

It might be better to keep the git part simple and block access to the .git directory by other means.

You can use .htaccess to deny public access to the .git directory

+4
Nov 13 '12 at 17:16
source share



All Articles