自动同步到远程git仓库 [英] Automatically synchronising to remote git repository

查看:290
本文介绍了自动同步到远程git仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种自动的方法可以将本地git存储库与远程git存储库进行同步以进行备份?

Is there an automatic way to synchronise a local git repository with a remote one, for backup?

基本上,我构想的结构是这样:

Basically the structure that I'm envisaging is this:

  • 中央服务器上的主"存储库
    • 用户A的PC具有主存储库的本地克隆
      • 备份服务器具有用户A的存储库的镜像
      • "Master" repository on a central server
        • User A's PC has a local clone of the master repo
          • Backup server has a mirror of user A's repo
          • 备份服务器具有用户B的存储库的镜像

          用户与主机之间的交互是手动的-用户必须选择何时推或拉更改.

          Interaction between the users and the master is manual -- the users have to choose when to push or pull changes.

          用户与其个人备份之间的交互是自动的-每当用户将任何更改(在任何分支上,包括创建新分支)提交到其本地存储库时,如果服务器可访问,都应立即将其推送到备份存储库中,并保持没有错误,直到服务器可访问为止(如果更容易,则在服务器可访问时执行下一个操作),然后将其推送. (即,它应该容忍断开连接的时间,但是在重新连接之后,存储库应该再次成为完整的镜​​像,包括断开连接时所做的更改.)

          Interaction between the users and their personal backup is automatic -- whenever the user commits any change (on any branch, including creating new branches) to their local repo it should immediately be pushed to the backup repo if the server is reachable, and otherwise held without error until the server is reachable (or the next action when the server is reachable, if that's easier) and then pushed. (ie. it should tolerate periods of disconnection but after reconnecting then the repos should be a complete mirror again, including changes made while disconnected).

          用户永远不需要自觉地推送到备份服务器.他们可能想从备份服务器上克隆一个新的仓库(例如,如果他们的硬盘驱动器死了,或者他们需要一段时间使用第二台PC).用户B可能希望从用户A的备份存储库中拉出一个分支(尽管这种情况很少见;通常,分支将通过主存储库进行交换).但是大多数情况下,它们应该对他们是不可见的.

          A user should never need to consciously push to the backup server. They might want to clone a new repo off the backup server (eg. if their hard drive dies or they need to use a second PC for a while). User B might want to pull a branch from user A's backup repo (though this will be rare; usually branches will be exchanged via the master repo). But mostly it should just be invisible to them.

          这应该是尽可能立即的,而不是计划中的任务,因为它们可能会提交然后关闭或与网络断开连接,在这种情况下,我仍然希望备份是最新的.

          And it should be as immediate as possible, not a scheduled task, because they might commit and then shut down or disconnect from the network, and I would still like the backup to be up to date in this case.

          设置主克隆和个人克隆很容易.但是如何设置此备份镜像?

          Setting up the master and personal clones is easy. But how do I set up this backup mirroring?

          此外,虽然仅镜像已提交的更改是可以的,但如果可以以某种方式还镜像未提交的更改(包括未版本控制但未被忽略的文件),则可能会很好.我可以不这样做,但可以(因为我怀疑它会变得凌乱).

          Also, while mirroring just the committed changes is ok, it might be nice if it could somehow also mirror uncommitted changes (including unversioned but not ignored files). I'm ok with not doing that though (as I suspect it'd get messy).

          推荐答案

          这应该包含提交后挂钩,该挂钩需要部署在每个客户端存储库中.
          或者该钩子可能是所有用户在使用git init时正在使用的git模板库的一部分:请参见 git init模板目录"部分.

          That should involve a post-commit hook, which needs to be deployed on each client repo.
          Or that hook could be part of a git template repo that all users are using when using git init: see git init "TEMPLATE DIRECTORY" section.

          该钩子将负责推送到备份裸仓库,并且可能像这个钩子一样简单.

          That hook would be in charge of pushing to a backup bare repo, and could be as simple as this one.

          #!/bin/sh 
          git push --mirror remote-name
          

          您会在"如何在git中提交后自动推送?中找到类似的想法.

          You will find similar ideas in "How to automatically push after committing in git?".

          这篇关于自动同步到远程git仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆