Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

git - How to set GIT_DIR & GIT_WORK_TREE when maintaining multiple repositories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...