简体   繁体   中英

Production, Staging, Dev websites and git repos on same server: how to setup GIT_DIR and GIT_WORK_TREE

I've read a number of posts that seem to get me close but not all the way to what I'd like. I have one server. On it, I have 3 websites: www.domain.com, staging.domain.com, and dev.domain.com. I also develop on local.domain.com and also use GitHub as my central repo.

I'd like to keep my git repositories out of my web roots. I'm open to how to do this. What I'm trying is:

/home/user/git/production.git 
/home/user/git/staging.git
/home/user/git/development.git

I know how to do this if there's only 1 site and repo by setting GIT_DIR and GIT_WORK_TREE (I export the paths in .bash_profile).

It seems I can set the working tree within each repository's config , like:

[core]
    bare = false
    worktree = /home/user/domains/www.domain.com/public_html

But how do I set GIT_DIR ?

So if I'm in /home/user/domains/ www .domain.com/public_html and do a git status , etc., it's referring to the production git repo. And if I'm in /home/user/domains/ staging .domain.com/public_html, it is tied to staging .git.

Rather than setting $GIT_DIR you could use gitlink files. To do this simply go into the root of each working tree and run:

echo "gitdir: /home/user/git/production.git" > .git

This would result in a .git file in your web root, but the only thing that would leak if somebody accessed that would be the path to the repository. This would avoid needing to play games with setting the $GIT_DIR variable based on which directory you're in.

What are you trying to do?

When in your /home/user/git/production.git it should detect the working directory as /home/user/domains/www.domain.com/public.html and push/pull/checkout should work fine there. Unfortunately, git is not psychic--from a random directory without a .git folder git doesn't even know where to look for a configuration file (without a "gitdir" file , as I just learned about from the other answer).

You can always pass in git --git-dir=~git/production.git checkout ... and so forth, but I bet you're looking for an automated solution.

Maybe try wrapping git in a shell script? What if you put this as ~/bin/autogit :

#!/bin/bash
DIR=$(pwd)
if (fgrep -q domains/www.domain.com <<< "$DIR"); then
  git --git-dir=/home/user/git/production.git $@
elif (fgrep -q domains/staging.domain.com <<< "$DIR"); then
  git --git-dir=/home/user/git/staging.git $@
elif (fgrep -q domains/local.domain.com <<< "$DIR"); then
  git --git-dir=/home/user/git/development.git $@
fi
echo "Autogit failed from directory $DIR."
exit 1      

(Side note: Why do you have three repos? I've found it more common to have one repo with three different branch names, and a script that checks out each branch to its appropriate directory.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM