简体   繁体   English

生产,登台,开发网站和git repos在同一台服务器上:如何设置GIT_DIR和GIT_WORK_TREE

[英]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上开发,并使用Gi​​tHub作为我的核心仓库。

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.

相关问题 如何在子模块中检测父 Git 存储库的正确 GIT_DIR 和 GIT_WORK_TREE - How to detect correct GIT_DIR and GIT_WORK_TREE of the parent Git repository in a submodule 如何在维护多个存储库时设置GIT_DIR和GIT_WORK_TREE - How to set GIT_DIR & GIT_WORK_TREE when maintaining multiple repositories Git 错误“致命:GIT_WORK_TREE(或 --work-tree=<directory> ) 不允许不指定 GIT_DIR (或 --git-dir=<directory> )” - Git error "fatal: GIT_WORK_TREE (or --work-tree=<directory>) not allowed without specifying GIT_DIR (or --git-dir=<directory>)" GIT_DIR 如何工作? 确切地 - How does GIT_DIR work? Exactly 如何使用git-bash设置GI​​T_WORK_TREE - How to set GIT_WORK_TREE with git-bash git:获取有效的GIT_DIR,如果当前目录是工作树子目录并且未设置$ GIT_DIR - git: get effective GIT_DIR, if current dir is a working tree subdir and $GIT_DIR is unset 使用$ GIT_DIR环境变量或--git-dir标志在同一目录中运行多个git repos是个坏主意吗? - Is it a bad idea to run multiple git repos in the same directory using the $GIT_DIR environment variable or --git-dir flag? GIT_WORK_TREE未按预期工作 - GIT_WORK_TREE is not working as expected 如何将“ GIT_WORK_TREE”设置为计算机中的特定驱动程序 - How to set 'GIT_WORK_TREE' to specific driver in computer 将git checkout中的子模块包含在挂钩的GIT_WORK_TREE中 - Including submodules in git checkout to GIT_WORK_TREE in hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM