[ACCEPTED]-What is GIT_WORK_TREE, why have I never needed to set this ENV var, why now?-git

Accepted answer
Score: 127

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

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

  • the 33 git directory.
    The git directory is normally 32 called .git, and is in the top level of your 31 working tree - this contains all the history 30 of your project, configuration settings, pointers 29 to branches, the index (staging area) and 28 so on.
    Your git directory is the one that 27 contains files and directories that look 26 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 25 above is the default layout of a git repository, you 24 can actually set any directories in the filesystem 23 to be your git directory and working tree.

You 22 can change these directories from their 21 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 20 of the first checks that git pull does - it must 19 be run from a working tree. I assume that 18 this is due to you having set the GIT_DIR or GIT_WORK_TREE environment 17 variables.
Otherwise, my best guess is that 16 your .git directory is not accessible or corrupted 15 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 14 the additional information you updated your 13 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 12 don't need to specify the work tree at all 11 1, so I would make sure that you unset the GIT_WORK_TREE and 10 GIT_DIR environment variables.

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

Score: 4

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

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

Score: 0

You may also run into this error if you've 8 renamed the path (working-tree) of a git 7 submodule. In my case I had updated the 6 path in .gitmodules to match my new path and thought 5 I was good. But when I did a git pull later it added 4 new files in the old path. This is because 3 there are two places the module path is 2 defined. You need to also update your "working-tree" as 1 defined in the .git/modules/{modulename}/config file.

More Related questions