在更新后 Hook 中使用以下代码时,是否可以包含子模块?
GIT_WORK_TREE=/path/to/directory git checkout -f
我还必须分发其他哪些选项,包括更新后 Hook 中的子模块?
谢谢。
最佳答案
问题“Using git submodule update --init
on a post hook”提到了在post-update
Hook 中使用该错误消息时可以看到的错误消息:
GIT_WORK_TREE=/path/to/directory git submodule update --init
这将给出:
remote: You need to run this command from the toplevel of the working tree.
因此,最好直接在目标存储库中使用
cd
并从此处运行命令:export GIT_DIR=$(pwd)
cd /path/to/target/workingtree
git checkout -f master
git submodule update --init --recursive
但是,如“How do I init/update a git submodule in a working tree after pushing to a bare working directory?”所示:
亚伦·亚当斯(Aaron Adams)的博客文章“Git push with submodules: a how-to guide”描述了与OP iliveinapark在the comments中显示的类似的错误消息:
fatal: This operation must be run in a work tree
git --git-dir=<my bare repo> --work-tree=<where I export to> submodule update --init --recursive
fatal: working tree '<where I export to>' already exists. Clone of '<submodule repo>' into submodule path '<submodule path>' failed
上面提到的博客文章提出了一种基于非裸仓库的方法(通常不建议将其推送到仓库,但在这种情况下是必要的):
使用Git管理带有子模块的网站:正确的方法
[aaron@aaronadams]$ cat > /usr/local/share/git-core/templates/hooks/post-receive.sample
#!/bin/sh
#
# An example hook script to update the working tree, including its
# submodules, after receiving a push.
#
# This hook requires core.worktree to be explicitly set, and
# receive.denyCurrentBranch to be set to false.
#
# To enable this hook, rename this file to "post-receive".
# Read standard input or hook will fail
while read oldrev newrev refname
do
:
done
# Unset GIT_DIR or the universe will implode
unset GIT_DIR
# Change directory to the working tree; exit on failure
cd `git config --get core.worktree` || exit
# Force checkout
git checkout --force
# Force update submodules
git submodule update --init --recursive --force
[aaron@aaronadams]$ chmod +x /usr/local/share/git-core/templates/hooks/post-receive.sample
[aaron@aaronadams]$ cd /var/www/vhosts/aaronadams.ca/sites/staging.aaronadams.ca [aaron@aaronadams]$ git init && git config --bool receive.denyCurrentBranch false && git config --path core.worktree ../ && mv .git/hooks/post-receive.sample .git/hooks/post-receive Initialized empty Git repository in /var/www/vhosts/aaronadams.ca/sites/staging.aaronadams.ca/.git/
[aaron@aaronadams]$ git remote set-url staging aaron@aaronadams.ca:sites/staging.aaronadams.ca
[aaron@aaronadams]$ git push staging master
remote: Submodule 'codeigniter' (git://github.com/EllisLab/CodeIgniter.git) registered for path 'codeigniter'
remote: Cloning into 'codeigniter'...
remote: Submodule path 'codeigniter': checked out 'fd24adf31255822d6aa9a5d2dce9010ad2ee4cf0'
To aaron@aaronadams.ca:sites/staging.aaronadams.ca
* [new branch] master -> master
OP iliveinapark mentions虽然: