在post-receive hook中的Git checkout:“Not a git repository'。'” [英] Git checkout in post-receive hook: "Not a git repository '.'"
问题描述
我在我的服务器上有一个非裸仓库(dirs / home / andrew / web和/home/andrew/web/.git),将receive.denyCurrentBranch设置为忽略并创建post-receive钩子:
#!/ bin / sh
GIT_WORK_TREE = / home / andrew / web git checkout -f
当我运行sh .git / hooks / post-receive时,一切正常。但是,当我从我的PC推送出现此错误:
remote:fatal:不是git存储库:'。'
有没有办法解决这个问题?最后,无需切换裸回购?
谢谢
编辑:这是我的新post-receive hook 。为什么在接受的答案中已经描述过这种情况。
echo\\\
Checking out $ PWD
GIT_DIR = / home / andrew / web / .git
GIT_WORK_TREE = / home / andrew / web git checkout -f
<解决方案
后接收钩子运行于 $ GIT_DIR
设为。
。这导致git寻找 ./ HEAD
, ./ refs / heads / master
等等,而不是 .git / HEAD
, .git / refs / heads / master
等。但是,由于您什么也没做在钩子中更改 $ PWD
(如图所示),钩子将运行在 .git
子目录中 /home/andrew/web/.git
),因此这个失败是相当神秘的:。
实际上是一个有效的git仓库。
避免硬编码路径名的一个标准技巧是使用 cd ..; git checkout -f
作为post-receive钩子。这是 $ GIT_DIR
的设置成为问题的地方,因为在 cd ..
之后,钩子正在运行假设是这种情况) / home / andrew / web
,当然在那个时候, $ GIT_DIR
应该是 .git
而不是。
。对此的标准修正仅仅是取消设置 GIT_DIR
(将其设置为 .git
也可以)。
尽管(对于适当的硬编码路径更改),但您显示的post-receive钩子对我来说工作正常。然后我再次从类似Unix的机器中推出,而不是个人电脑。是否有可能发生其他事情,将目录从 .git
子目录中更改?您可以在$ hook中运行 echo中运行的$ PWD
来查看您的位置。
I have a non-bare repository at my server (dirs /home/andrew/web and /home/andrew/web/.git), set receive.denyCurrentBranch to ignore and created post-receive hook:
#!/bin/sh
GIT_WORK_TREE=/home/andrew/web git checkout -f
When I run sh .git/hooks/post-receive, everything works fine. But when I push from my PC I get this error:
remote: fatal: Not a git repository: '.'
Is there a way how can I solve this issue? And eventually without having to switch bare repo?
Thank you
EDIT: Here's my new post-receive hook. Why it is like this has been described in the accepted answer.
echo "\nChecking out $PWD"
GIT_DIR=/home/andrew/web/.git
GIT_WORK_TREE=/home/andrew/web git checkout -f
[Edit, Feb 2017: this old answer still gets hit a bit, so let's add a few notes. (1) This kind of live update is often a bad idea: be sure you know why you're doing it, and that you won't clobber your own work. (2) In Git since 2.3, you can now configure receive.denyCurrentBranch
to updateInstead
, plus a hook tweak in Git 2.4 and later. For details, see the git config
documentation.]
The post-receive hook is run with $GIT_DIR
set to .
. This causes git to look for ./HEAD
, ./refs/heads/master
, etc., rather than .git/HEAD
, .git/refs/heads/master
, etc. But, since you don't do anything to change $PWD
in the hook (as shown anyway), the hook will be running in the .git
subdirectory (/home/andrew/web/.git
), and hence this failure is quite mysterious: .
will in fact be a valid git repository.
One standard trick that avoids hard-coding the path name is to use cd ..; git checkout -f
as the post-receive hook. This is where the setting of $GIT_DIR
becomes a problem, because after cd ..
the hook is running in (still assuming this case) /home/andrew/web
and of course at that point, $GIT_DIR
should be .git
rather than .
. The standard fix for that is simply to unset GIT_DIR
(setting it to .git
would also work).
Your post-receive hook as shown works fine for me, though (with appropriate hard-coded-path changes). Then again I am pushing from a Unix-like machine, not a PC. Is it possible there's something else happening, that changes directories out of the .git
subdirectory? You can do something like echo running in $PWD
in the hook to see where you are.
这篇关于在post-receive hook中的Git checkout:“Not a git repository'。'”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!