远程裸机上的Git子模块

Thi*_*lem 23 git git-submodules git-bare

我已经设置了我的环境,所以我可以推送到远程裸存储库,我使用这些命令来设置远程存储库:

$ mkdir ~/website.git && cd ~/website.git
$ git init --bare
Run Code Online (Sandbox Code Playgroud)

$ cat > hooks/post-receive

#!/bin/sh
GIT_WORK_TREE=/var/www/website git checkout -f

$ chmod +x hooks/post-receive
Run Code Online (Sandbox Code Playgroud)

在我的本地环境中:

$ git remote add web ssh://website.com/home/website.git
$ git push web +master:refs/heads/master
Run Code Online (Sandbox Code Playgroud)

现在我可以部署到这个远程使用git push web,一切都很好..

问题:子模块

我的项目中有一些子模块没有在远程存储库中初始化/更新...我无法git submodule update在裸机上运行,因为它是裸的,我不能在/var/www/website文件夹上运行它,因为它只是一个副本的文件而不是git repo.

小智 10

我想出了另一个看起来很干净的解决方案.只需给git执行子模块所需的所有信息:

$ cd /path/to/your/git_work_tree
$ git --git-dir=/path/to/your/bare_repo.git --work-tree=. submodule init
$ git --git-dir=/path/to/your/bare_repo.git --work-tree=. submodule update
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 9

一种可能的方式可能是:

换句话说:
从裸仓库中取出而不是试图从裸仓库中结账:非裸仓库应该能够适应这一git submodule update步骤.

示例脚本可能看起来像

#!/bin/sh

# Get the latest code
cd /path/to/bare/repo

# Set git variables
GIT_WORK_TREE=/var/www/website
GIT_DIR=/var/www/website/.git

# Go to website and pull
cd /var/www/website
git pull /path/to/bare/repo
git submodule update --init --recursive

# Run additional build stuff here
Run Code Online (Sandbox Code Playgroud)


Aar*_*ams 6

两天前,当我遇到同样的问题时,我偶然发现了这个帖子.在最终找到一个漂亮,整洁的解决方案之后,我在这里写了一篇关于它的文章:

Git push with submodules:一个操作指南

我意识到,如果我要push到裸回购,只使用post-receivepull到非裸露的回购协议,我还不如干脆把它简单,push直接将非纯仓库.这是一个明显的案例,其中只推动裸仓的"最佳实践"只会增加复杂性.

如果链接腐烂,我会在这里粘贴我的解决方案,跳过我遇到的所有相同问题,我相信你做的.


首先,让我们创建一个通用 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/
Run Code Online (Sandbox Code Playgroud)

最后,在我们的本地机器上,我们将更改我们的遥控器以反映新存储库的位置,并推送.

[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)

神圣的废话,它的工作!

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


我希望这个答案可以帮助别人,就像过去两天其他人的Stack Exchange帖子一样帮助我!

  • @ jrhorn424的确,上述解决方案存在很多问题; 自从我写这篇文章以来,我学到了很多东西,最重要的是子模块会产生比他们解决的问题更多的问题.我强烈建议切换到我在此处记录的无子模块解决方案:http://stackoverflow.com/questions/14656047/how-do-i-add-upgrade-downgrade-a-remote-git-project-在-我的存储库,而无需-USI (2认同)