且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动镜像一个git仓库

更新时间:2023-11-30 14:29:46

我为此写了一个post-commit钩子。钩本身很简单,只需在 .git / hooks / 目录中添加一个名为 post-commit 的文件,其内容如下:

  git push my_remote 



post-commit 文件应该是可执行文件。另外请确保您添加了合适的远程 my_remote 这个钩子才起作用。

我还制作了一个名为后合并,指向后提交。这是可选的。如果你这样做,你会在合并后自动同步。

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

  git push my_remote -f --mirror 


One of the side-effects of using an external Subversion repository was getting automatic offsite backups on every commit.

I'd like to achieve the same using 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?

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:

git push my_remote

The post-commit file should be executable. Also make sure that you add a suitable remote repository with the name my_remote for this this hook to work.

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.

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:

git push my_remote -f --mirror