git 维护多个存储库时如何设置 GIT_DIR 和 GIT_WORK_TREE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10821825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set GIT_DIR & GIT_WORK_TREE when maintaining multiple repositories
提问by doub1eHyman
Much like this SO post, I have the following workflow that i would like to maintain with git:
很像这个 SO post,我有以下工作流程,我想用 git 维护:
- Multiple people develop locally
- Commit a few things
- Commit more things
- Push to Staging
- Test new code on Staging
- Push to Production
- 多人本地开发
- 承诺一些事情
- 提交更多的东西
- 推送到登台
- 在 Staging 上测试新代码
- 推向生产
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.
我们的临时服务器为许多不同的站点提供服务。我想在用户主目录的不同文件夹中为每个设置存储库。每个存储库都将有一个 post-receive hook(ala the Daniel Messier 文章),用于检查该站点/存储库的 Web 根目录中的更改。
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).
当我尝试运行任何 git 命令时,出现错误“致命:此操作必须在工作树中运行”。无论我是从存储库(/home/gituser/website1.git - 我认为无论如何都不应该工作..)或从网络根目录(/var/www/website1)运行“git status”,我都会收到此错误.
However, if I specify the GIT_DIR and GIT_WORK_TREE, then I am able to run git commands. Both of these work:
但是,如果我指定 GIT_DIR 和 GIT_WORK_TREE,那么我就可以运行 git 命令。这两个工作:
$ 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:
所以我需要:
- An alternative to typing the directory and work tree along with every command
- An alternative to setting them as persistent environment variables, because that would only work for website1, not website2, website3, etc
- 输入目录和工作树以及每个命令的替代方法
- 将它们设置为持久环境变量的替代方法,因为这仅适用于网站 1,而不适用于网站 2、网站 3 等
Am I going about this correctly? How can I get git the directory info it needs for each repository?
我这样做正确吗?如何获取每个存储库所需的 git 目录信息?
回答by ellotheth
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
:
如果我理解正确的话,每个 Web 根目录都有一个不同的存储库。如果是这种情况,您可以在存储库级别(在.git/config
文件中)设置工作树:core.worktree
. 如果 repo 配置为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.)
完成这些设置,Git命令(像git status
)应该从存储库文件夹再次合作,并使用该工作树(任何命令git status
,git checkout
等等)将使用core.worktree
位置。(请注意,Git 命令仍然无法从工作树位置工作,因为它不是存储库。)
回答by selbie
Just a suggestion.
只是一个建议。
Implement a script file called "git" that is placed in a directory that preceeds the user's path before the git executable in /usr/bin. git, the shell script, is just a thin wrapper on top of /usr/bin/git. The first thing it does is detect what directory git is being called from and tries to auto-infer what repo it is working in. Perhaps by having a "my.config" file in the root of each repository that the script can read. Then the script calls "/usr/bin/git --git-dir" and sets GIT_DIR and GIT_WORK_TREE vars and then calls /usr/bin/git with the same command line params.
实现一个名为“git”的脚本文件,该文件放置在 /usr/bin 中 git 可执行文件之前的用户路径之前的目录中。git,shell 脚本,只是 /usr/bin/git 之上的一个薄包装。它做的第一件事是检测正在调用 git 的目录,并尝试自动推断它在哪个存储库中工作。也许通过在脚本可以读取的每个存储库的根目录中有一个“my.config”文件。然后脚本调用“/usr/bin/git --git-dir”并设置 GIT_DIR 和 GIT_WORK_TREE 变量,然后使用相同的命令行参数调用 /usr/bin/git。
回答by FractalSpace
Three options:
三个选项:
1) Add a local alias:
1)添加本地别名:
$ git config alias.root '!pwd'
$ git config alias.root '!pwd'
2) Add a global alias ($HOME/.gitconfig):
2)添加全局别名($HOME/.gitconfig):
[alias]
findroot = "!f () { [[ -d ".git" ]] && echo "Found git in [`pwd`]" && exit 0; cd .. && echo "IN `pwd`" && f;}; f"
3) A script, findgitroot.sh e.g
3)一个脚本,findgitroot.sh 例如
#!/bin/sh
f () { [[ -d ".git" ]] && echo "Found git in [`pwd`]" && exit 0; cd .. && f;}
f
回答by Adam Dymitruk
You can always press up arrow to get all that info and then just delete the command and replace with another one. You shouldn't need to retype the whole thing.
您可以随时按向上箭头获取所有信息,然后只需删除该命令并替换为另一个命令。您不应该需要重新输入整个内容。
If you've issued a number of other commands and don't want to "up arrow" through history too much, you can press CTRL-R and start typing "tree" or "work". You should get the point in history where one of these was run.
如果您已经发出了许多其他命令并且不想在历史记录中“向上箭头”太多,您可以按 CTRL-R 并开始输入“树”或“工作”。您应该了解其中一个运行的历史点。
Otherwise, script setting those variables as already suggested.
否则,按照已经建议的方式设置这些变量的脚本。