问题描述
我有一个很大的私有存储库,该存储库保存在本地网络上.我想自动将该存储库的子树推送到该网络之外.我需要它很简单:
I have a big private repository which is maintained on a local network. I'd like to automatically push a subtree of that repository outside of that network. I need it to be simple:
*任务* 有人推送到本地远程存储库->子树会自动推送到其他存储库
*Task* someone pushes to local remote repository --> a subtree is automatically pushed to some other repository
我不确定这是否可以通过服务器端挂钩来实现,因为AFAIK不存在从裸机远程推送 subtrees 的问题.我提出了两个想法:
I am not sure if this could be achieved with a server side hook because AFAIK there is no such thing as pushing subtrees from bare remotes. I came up with two ideas:
- 我可以在服务器上克隆远程服务器,并自动在克隆的远程服务器中拆分子树.这真的没有帮助,因为我不知道如何自动拉子树(其他人也有 this问题).
- 另一个想法是编写一个自定义客户端
post-commit-hook
并让每个用户安装它,但这很糟糕,不是吗? Git书明确指出,政策应在服务器端强制执行.
- I could clone the remote on the server and automatically split the subtree in the cloned remote. This doesn't really help because I don't know how to auto-pull the subtree (others also have this problem).
- Another idea is to write a custom client-side
post-commit-hook
and make every user install it, but this is terrible, isn't it? Git book specifically states that policies should be enforced on server side.
有没有简单的方法可以实现这样的目标?还是这不可能,而仅仅是git滥用?
Is there a simple way of achieving something like this? Or is this impossible and it's just git abuse?
推荐答案
嗯,我有点不好意思.显然,这比我想象的要容易得多.这是一个基于 @wrzasa 建议的草草解决方案:
Umm, I'm a bit embarrassed. Apparently this was much easier than I thought. Here is a hasty solution which builds on @wrzasa suggestion:
-
将您的存储库克隆到要推送到的服务器上,如下所示(dir.git是一个裸仓库):
Clone your repository on the server to which you are pushing, like this (dir.git is a bare repo):
.
|- dir.git
|- dir
在dir
中执行:git remote add <remote-name> <remote-address>
In dir
do: git remote add <remote-name> <remote-address>
在dir.git/hooks/post-receive
中放置:
#! /bin/bash
unset GIT_DIR
cd ../dir
git pull ../dir.git
git subtree split --prefix=<subdir-in-dir> --branch=<branch-name>
git push <remote-name> <branch-name>
记住要使post-receive
可执行.如果您想知道为什么需要unset GIT_DIR
,请参见此答案.
Remember to make post-receive
executable. See this answer if you wanna know why unset GIT_DIR
is needed.
就是这样.现在,只要有人推送到<subdir-in-dir>
下的dir
远程(即dir.git
)子树,就会被推送到<remote-name>
.
That's pretty much it. Now whenever someone pushes to dir
remote (i.e. dir.git
) subtree under <subdir-in-dir>
will be pushed to <remote-name>
.
这篇关于Git:有没有一种方法可以自动推送子树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!