Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set GIT_DIR & GIT_WORK_TREE when maintaining multiple repositories

Tags:

git

repository

Much like this SO post, I have the following workflow that i would like to maintain with git:

  1. Multiple people develop locally
  2. Commit a few things
  3. Commit more things
  4. Push to Staging
  5. Test new code on Staging
  6. Push to Production

Our staging server serves many different sites. I would like to set up the repository for each in a different folder in a user's home directory. Each repository will have a post-receive hook (ala the Daniel Messier article) that checks out the changes into the web root for that site/repository.

It's number six that is giving me trouble.

When I try to run any git commands, I get the error "fatal: This operation must be run in a work tree". I get this error whether I am running 'git status' from the repository (/home/gituser/website1.git - which I don't believe should work anyway..) or from the web root (/var/www/website1).

However, if I specify the GIT_DIR and GIT_WORK_TREE, then I am able to run git commands. Both of these work:

$ git --git-dir /home/gituser/website1.git --work-tree /var/www/website1 status
$ GIT_DIR=/home/gituser/website1.git GIT_WORK_TREE=/var/www/website1 git status 

So I need:

  1. An alternative to typing the directory and work tree along with every command
  2. An alternative to setting them as persistent environment variables, because that would only work for website1, not website2, website3, etc

Am I going about this correctly? How can I get git the directory info it needs for each repository?

like image 998
doub1ejack Avatar asked May 30 '12 18:05

doub1ejack


People also ask

What is GIT_DIR?

GIT_DIR is the location of the . git folder. If this isn't specified, Git walks up the directory tree until it gets to ~ or / , looking for a . git directory at every step. GIT_CEILING_DIRECTORIES controls the behavior of searching for a .

How do I change my working directory in git?

To change this current working directory, you can use the "cd" command (where "cd" stands for "change directory"). For example, to move one directory upwards (into the current folder's parent folder), you can just call: $ cd ..

How do I initialize a new git repository?

Initializing a new repository: git init To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new . git subdirectory in your current working directory.


1 Answers

If I understand correctly, you've got a different repository for each web root. If that's the case, you can set the work tree at the repository level (in the .git/config file): core.worktree. It won't work if the repo is configured as bare:

[core]
    bare = false
    worktree = /webroot/dir/for/this/repo

Once that's set, Git commands (like git status) should work from the repository folder again, and any commands that use the work tree (git status, git checkout, etc.) will use the core.worktree location. (Note that Git commands still won't work from the work tree location, because it's not a repository.)

like image 67
ellotheth Avatar answered Oct 13 '22 01:10

ellotheth