What is GIT_WORK_TREE, why have I never needed to set this ENV var, why now?

If you have a non-bare git repository, there are two parts to it:

  • the working tree.
    The working tree has your checked out source code, with any changes you might have made.

  • the git directory.
    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.


Perhaps it's better to keep the git part simple and deny access to the .git directory with other means.

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

Tags:

Git