自动镜像一个 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:31:43  来源:igfitidea点击:

Automatically mirror a git repository

gitversion-controldvcs

提问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-committo your .git/hooks/directory with the following contents:

我为此编写了一个提交后挂钩。钩子本身很简单;只需将一个名为的文件添加post-commit到您的.git/hooks/目录中,内容如下:

git push my_remote

The post-commitfile should be executable. Also make sure that you add a suitable remoterepository with the name my_remotefor this this hook to work.

post-commit文件应该是可执行的。还要确保您添加了一个合适的远程存储库,并my_remote使用此挂钩的名称来工作。

I also made a symlink named post-mergethat 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-commithook can use:

更新:如果你想确保你的服务器和你的镜像不会不同步,并确保所有的分支也都被备份,你的post-commit钩子可以使用:

git push my_remote -f --mirror