Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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

I have a 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've created another repo for my other website 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, and 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.

like image 575
Joern Akkermann Avatar asked Mar 12 '11 15:03

Joern Akkermann


People also ask

Where is the work tree in Git repository?

The repository’s work tree is the root directory of this project, i.e., ~/projects/foo. When we use git command such as git status inside in git repository, git will find .git directory in the directory or upward until it finds the .git directory.

When to create a throwaway working tree in Git?

On the other hand, if you just plan to make some experimental changes or do testing without disturbing existing development, it is often convenient to create a throwaway working tree not associated with any branch. For instance, git worktree add -d <path> creates a new working tree with a detached HEAD at the same commit as the current branch.

How many working trees can you have in Git?

We will cover how to do each of these in the Git worktree examples later in this article. It is important to know that whatever path you choose, you can only ever have one working tree per directory. When you create the new working tree, the target directory must be empty, otherwise, the command will fail and you will get an error message.

What does Git worktree add-d-d do?

For instance, git worktree add -d <path> creates a new working tree with a detached HEAD at the same commit as the current branch.


2 Answers

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.

like image 185
Mark Longair Avatar answered Sep 22 '22 16:09

Mark Longair


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

like image 33
crd23 Avatar answered Sep 20 '22 16:09

crd23