在 2 个 git 存储库之间同步

发布于 2024-10-30 15:14:27 字数 1312 浏览 1 评论 0 原文

我有 2 个裸存储库。它们的制作方式如下:

ssh [email protected]
git init --bare repo1
ssh [email protected]
git clone --bare [email protected]:repo1

一个用于开发(我们称之为),一个用于备份(以防第一个无法访问)。是否可以自动同步它们 - 就像在备份上执行 git pull 一样。

我猜你无法合并或拉取裸存储库。是否有另一种方法可以使备份存储库保持最新,而不是这样:

ssh [email protected]
rm repo1 -fr
git clone -- bare [email protected]:repo1

当然,当主库在一段时间内无法访问并且我使用备份时,我会想要更新主库。

另外,向工作存储库添加 2 个遥控器也是一种解决方案,但您必须不断推送这两个遥控器,如果其中一个无法访问,则不会发生这种情况。

所有冲突都在非裸存储库中解决

编辑为什么我需要备份存储库:

我们使用远程存储库来交换代码,并且每天都需要它。通常人们不需要其他开发人员编写的代码,但情况并非总是如此。我们和初级失去联系3天了,发展起来并不容易。我在另一台服务器上创建了第二个存储库,并在本地进行了克隆,但我必须为很多项目执行此操作,而且非常耗时。我更喜欢自动更新第二个存储库。

I have 2 bare repositories. They are made like this:

ssh [email protected]
git init --bare repo1
ssh [email protected]
git clone --bare [email protected]:repo1

One is used for development (let's call it primary) and one is used for backup (in case first is not accessible). Is it possible to automatically synchronize them - something like doing git pull on backup.

I guess you can't merge or pull on bare repository. Is there another way to have backup repository up to date, rather than this:

ssh [email protected]
rm repo1 -fr
git clone -- bare [email protected]:repo1

of course when primary wasn't accessible for a while and I used backup then I would want to update primary.

Also adding 2 remotes to the working repository is a solution, but you have to constantly push to the both of them, which can't happen if one is inaccessible.

All conflicts are resolved in the non-bare repositories

edit why do I need backup repository:

we use remote repository to exchange code and it's needed daily. usually people don't need code written by other developers, but that's not always the case. we lost contact with primary for 3 days and it was not easy to develop. I made second repository on another server and I cloned local, but I had to do that for a lot of projects and it's time consuming. I prefer to have the second repository automatically updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-11-06 15:14:27

您可以使用 git push --mirror other-remote 将一个存储库中的所有引用镜像到另一个存储库。不过,这不会镜像存储库配置或存储 - 您可能还想看看这个问题的答案:


更新:为了回应您在下面澄清的问题和评论,我应该说,在两个方向上 git push --mirror 都是不安全的,因为 git push --mirror 会执行 强制更新引用并删除已删除的引用。例如,假设 primary 上的 master 领先于 backup 上的 master,并且 primary 上有一个新分支尚未镜像 - 那么 git push --mirror 会将 primary 上的 master 分支重置为旧状态并删除新状态创建了分支。

此外,即使您仅从 primary 镜像到 backup,允许人们推送到 backup 也是不安全的,因为任何新的内容仅仅推到那里就会被下一个镜子移除。

那么,你能做什么呢?我假设当主存储库不可用时,整个机器都不可用。因此,在备份服务器上,我将创建一个裸存储库,正如您所建议的那样,主服务器上的 cron 作业将镜像该存储库。但是,您不应该允许人们推送到该镜像存储库 - 当主服务器出现故障时,克隆镜像以创建一个人们可以同时推送到的裸存储库。当主服务器备份时,阻止人们推送到备份上的新存储库,然后在本地合并更改并将新工作推送回主服务器。

You can use git push --mirror other-remote to mirror all the refs in one repository to another. This won't mirror the repository config or stashes, though - you might want to also look at this question's answers:


Update: In response to your clarified question and comment below, I should say that it's not safe to git push --mirror in both directions, since git push --mirror does a forced update of refs and removes deleted refs. For example, suppose your master on primary is ahead of that on backup and there's a new branch on primary that hasn't been mirrored yet - then a git push --mirror will reset the master branch on primary to an old state and delete the newly created branch.

In addition, even if you're only mirroring from primary to backup, it's not safe to allow people to push to backup, since anything new pushed solely to there would be removed by the next mirror.

So, what can you do? I'm assuming that when the primary repository is unavailable, that whole machine is unavailable. So, on the backup server, I would create one bare repository, as you suggest, that a cron job on primary will mirror the repository to. You shouldn't allow people to push to that mirrored repository, however - when the primary server goes down, clone the mirror to create a bare repository that people can push to in the mean time. When the primary server is back up, stop people from being able to push to that new repository on the backup, then merge the changes from there locally and push the new work back to the primary server.

糖粟与秋泊 2024-11-06 15:14:27

使用 crontab 脚本或类似脚本定期运行 git push --mirror 远程备份。您还可以添加一个提交挂钩,以在更新时自动强制推送到另一个远程,这样您就不会出现“失败窗口”。无需从备份进行更新,因为它可能永远不会有任何新的或更新的内容。

Use a crontab script or similar to run git push --mirror remote to your backup on a regular basis. You could also add a commit hook to automatically force push to another remote on updates, this way you don't have a "failure window". No need to update from backup since it will presumably never have any new or updated content of it's own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文