[英]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. 在它上面,我有3个网站:www.domain.com,staging.domain.com和dev.domain.com。 I also develop on local.domain.com and also use GitHub as my central repo. 我也在local.domain.com上开发,并使用GitHub作为我的核心仓库。
I'd like to keep my git repositories out of my web roots. 我想把我的git存储库从我的网络根源中删除。 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). 我知道如果通过设置GIT_DIR和GIT_WORK_TREE(我在.bash_profile中导出路径)只有1个站点和repo,如何做到这一点。
It seems I can set the working tree within each repository's config
, like: 我似乎可以在每个存储库的config
设置工作树,例如:
[core]
bare = false
worktree = /home/user/domains/www.domain.com/public_html
But how do I set GIT_DIR
? 但是我该如何设置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. 因此,如果我在/ home / user / domains / www .domain.com / public_html并执行git status
等,则它指的是生产 git repo。 And if I'm in /home/user/domains/ staging .domain.com/public_html, it is tied to staging .git. 如果我在/ home / user / domains / staging .domain.com / public_html中,则它与staging .git绑定。
Rather than setting $GIT_DIR
you could use gitlink files. 您可以使用gitlink文件而不是设置$GIT_DIR
。 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. 这会在您的Web根.git
中生成一个.git
文件,但如果有人访问该文件,那么唯一会泄漏的就是存储库的路径。 This would avoid needing to play games with setting the $GIT_DIR
variable based on which directory you're in. 这将避免需要根据您所在的目录设置$GIT_DIR
变量来玩游戏。
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. 在/home/user/git/production.git
它应该检测工作目录为/home/user/domains/www.domain.com/public.html
并且push / pull / checkout应该在那里正常工作。 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). 不幸的是,git不是通灵的 - 来自没有.git
文件夹的随机目录git甚至不知道在哪里查找配置文件(没有“gitdir”文件 ,正如我刚从其他答案中了解到的那样)。
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. 你总是可以传入git --git-dir=~git/production.git checkout ...
等等,但我敢打赌你正在寻找一种自动解决方案。
Maybe try wrapping git in a shell script? 也许尝试在shell脚本中包装git? What if you put this as ~/bin/autogit
: 如果你把它作为~/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.) (旁注:为什么你有三个repos?我发现有一个带有三个不同分支名称的repo,以及一个将每个分支检出到其相应目录的脚本更常见。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.