如何将Git代码库同步到镜像库?

8

我想将 Git 仓库镜像到本地仓库,并希望及时同步它们,我像这样做:

git clone --mirror source_url source_repo
cd source_repo
git push --mirror local_url

现在我已经将源代码库镜像到本地代码库,我可以像这样克隆我的本地代码库:
git clone local_url local_repo

但是由于源代码库正在更新,我该如何保持它们同步?

5个回答

3
任何本地存储库,无论是镜像还是工作的,都可以通过git remote update命令进行同步。
cd REPO
git remote update

2
为了实现这个目的,我创建了git-sync-mirror。最初的回答。

git-sync-mirror

A simple synchronization container image for git repositories over HTTPS

Features:

  • Authentication with https tokens
  • If needed, use a different HTTPS Proxy for source and destination
  • TLS-Trust On First Use: Seamlessly run this container behind a https scanning proxy
  • Skip certificate checks (don't do that)
  • Configure time to sleep between synchronization attempts

Usage

$ docker run \
   --rm \
   --env SRC_REPO=source \
   --env DST_REPO=destination \
   --env SLEEP_TIME=30s \
   enteee/git-sync-mirror

Note: The container is designed for synchronization over https with supported authentication using access tokens. For example replace source with https://github-user:github-access-token@github.com/Enteee/git-sync-mirror.git


1

这里有几种方法。您可以选择方便的一种。

  1. https://softwareengineering.stackexchange.com/questions/195456/keeping-git-repositories-on-different-hosts-in-sync中讨论的方法(编写脚本等)
  2. 向您的git仓库添加自定义挂钩 - https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
  3. 如果您正在使用像BitBucket这样的第三方工具,则应该具有内置解决方案或插件。例如:https://marketplace.atlassian.com/plugins/com.englishtown.stash-hook-mirror/server/overview

0

我多年来一直使用以下方案:

  • 在一个远程服务器(server1)上为我的分布式客户端创建一个或多个主git存储库。所有非本地用户(在serverX上)都会将所需的存储库克隆到本地并将更新推送到主存储库。

  • 在另一个远程服务器(server2)上,我将主存储库克隆为镜像。然后我设置了一个cron作业定期运行“cd REPO; git remote update”。该远程服务器目前正在Digital Ocean上设置,并且我已经安排了每周备份。过去,我曾在至少两个不同的远程服务器上镜像主存储库以确保安全性。


0
在我的情况下,我想要在从本地仓库推送后同步两个远程仓库(A和B)。
Local repository pushes to remote repository "A"
Remote repository "A" pushes to remote repository "B"

为了避免为每个本地仓库都添加两个仓库,我在远程仓库“A”上创建了一个 post-update 钩子,并只用了一行代码:
git push --all --force

现在部署的远程仓库“B”始终与中央远程仓库“A”自动保持最新。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接