自动镜像一个 git 仓库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3583061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically mirror a git repository
提问by Andy Baker
One of the side-effects of using an external Subversion repository was getting automatic offsite backups on every commit.
使用外部 Subversion 存储库的副作用之一是每次提交时都会自动进行异地备份。
I'd like to achieve the same using Git.
我想使用 Git 实现相同的目标。
i.e. every commit to my local repository automatically commits to an external one so the two repositories are always in sync.
即每次提交到我的本地存储库都会自动提交到一个外部存储库,因此这两个存储库始终同步。
I imagine that a post-commit hook would be the way to go. Does anyone have any specific examples of this?
我想一个提交后的钩子将是要走的路。有没有人有任何具体的例子?
采纳答案by Manoj Govindan
I wrote a post-commit hook for just this purpose. The hook itself is simple; just add a file named post-commit
to your .git/hooks/
directory with the following contents:
我为此编写了一个提交后挂钩。钩子本身很简单;只需将一个名为的文件添加post-commit
到您的.git/hooks/
目录中,内容如下:
git push my_remote
The post-commit
file should be executable. Also make sure that you add a suitable remoterepository with the name my_remote
for this this hook to work.
该post-commit
文件应该是可执行的。还要确保您添加了一个合适的远程存储库,并my_remote
使用此挂钩的名称来工作。
I also made a symlink named post-merge
that points to post-commit
. This is optional. If you do this you'll auto-sync after merges as well.
我还创建了一个符号链接,名为post-merge
指向post-commit
. 这是可选的。如果你这样做,你也会在合并后自动同步。
UPDATE:If you want to ensure that your server, and your mirror do not get out of sync, and ensure that all branches are also backed up, your post-commit
hook can use:
更新:如果你想确保你的服务器和你的镜像不会不同步,并确保所有的分支也都被备份,你的post-commit
钩子可以使用:
git push my_remote -f --mirror