将git checkout中的子模块包含在挂钩的GIT_WORK_TREE中

ili*_*ark 5 git git-checkout githooks

在更新后的挂钩中使用以下代码时是否可以包含子模块?

GIT_WORK_TREE=/path/to/directory git checkout -f
Run Code Online (Sandbox Code Playgroud)

我还有什么其他选择来分发代码,包括更新后挂钩的子模块?

谢谢.

Von*_*onC 11

这个问题:" 使用git submodule update --init上后钩 "中提到的错误消息,如果你使用这个,你可以看到post-update钩:

GIT_WORK_TREE=/path/to/directory git submodule update --init
Run Code Online (Sandbox Code Playgroud)

这会给:

remote: You need to run this command from the toplevel of the working tree.
Run Code Online (Sandbox Code Playgroud)

所以最好cd直接在目标仓库中并从那里运行命令:

export GIT_DIR=$(pwd)

cd /path/to/target/workingtree

git checkout -f master
git submodule update --init --recursive
Run Code Online (Sandbox Code Playgroud)

但是,如" 在推送到裸工作目录后如何在工作树中初始化/更新git子模块? "中所示:

看起来当您运行"git submodule update"时无法设置GIT_WORK_TREE:
它会尝试将其用作子模块的工作树,而不是超级项目.

Aaron Adams 的博客文章" Git push with submodules:how-to guide "描述了类似的错误消息,如OP iliveinapark评论中所示:

可悲的是,这不起作用,我怀疑是因为我的回购是一个简单的回购.
我按照这些命令得到的错误是:

fatal: This operation must be run in a work tree
Run Code Online (Sandbox Code Playgroud)

如果,为了克服上述错误,我使用类似的东西:

git --git-dir=<my bare repo> --work-tree=<where I export to> submodule update --init --recursive 
Run Code Online (Sandbox Code Playgroud)

我明白了:

fatal: working tree '<where I export to>' already exists. Clone of '<submodule repo>' into submodule path '<submodule path>' failed
Run Code Online (Sandbox Code Playgroud)

上面提到的博客文章提出了一种基于非裸仓库的方法(通常推荐用于推送,但在这种情况下是必要的):

使用Git管理带有子模块的网站:正确的方法

首先,让我们创建一个通用的post-receive钩子,我不需要在每个存储库的基础上进行更改:

[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
Run Code Online (Sandbox Code Playgroud)

现在让我们继续并打破所有规则.

我们要:

  • 在我们的网站目录中初始化一个非裸Git存储库;
  • 确保它可以从git push接收;
  • 显式将其工作树设置为其父目录;
  • 并启用我们刚刚创建的钩子.
[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
Run Code Online (Sandbox Code Playgroud)

神圣的废话,它的工作!

这种方法不仅与子模块兼容,而且只需要一个命令来设置一个新的远程存储库(好的,它包含四个命令).
它还将存储库和工作树保持在同一个位置; 并且在我们的配置或钩子文件中不需要绝对路径,它现在也完全可移植.


iliveinapark OP 提到,虽然:

然而,这变得有点过于繁琐,所以我选择了简单的强制结账,并将手动更新我的子模块.