Git - 带有 git pull 的 post-receive 钩子“找不到有效的 git 目录”

zan*_*ona 18 unix git hook

这很奇怪,但是在设置 git 存储库并使用以下命令创建 post-receive 挂钩时:

echo "--initializing hook--"
cd ~/websites/testing
echo "--prepare update--"
git pull
echo "--update completed--"
Run Code Online (Sandbox Code Playgroud)

钩子确实运行了,但它永远无法正确运行 git pull :

6bfa32c..71c3d2a  master -> master
--initializing hook--
--prepare update--
fatal: Not a git repository: '.'
Failed to find a valid git directory.
--update completed--
Run Code Online (Sandbox Code Playgroud)

所以我现在问自己,如何让钩子用 post-receive 更新克隆?

在这种情况下,运行进程的用户是相同的,它的所有内容都在用户文件夹中,所以我真的不明白......因为如果我手动进入

cd ~/websites/testing
git pull
Run Code Online (Sandbox Code Playgroud)

它工作没有任何问题......

对此的任何帮助将不胜感激

非常感谢

Tob*_*obu 26

当钩子运行时,GIT_DIR并且(如果明确定义了工作树)GIT_WORK_TREE被设置。这意味着您的拉取不会与您更改的目录中的第二个存储库一起运行。

尝试git --git-dir ~/websites/testing/.git --work-tree ~/websites/testing pull; 或者用这个取消 git 的 repo-local 环境:

unset $(git rev-parse --local-env-vars)
Run Code Online (Sandbox Code Playgroud)

man 1 git 中有关这些环境变量的更多信息。


小智 6

我经历的一件事是,使用post-update钩子 '--git-dir' 效果很好,但 git 仍然抱怨缺少工作树(尽管使用了 '--work-tree')

简而言之,这不起作用:

git --git-dir /path/to/websites/testing/.git --work-tree /path/to/websites/testing pull

而这有效:

cd /path/to/websites/testing
git --git-dir /path/to/websites/testing/.git pull