6

Gitea 与 GitHub 仓库同步

 2 years ago
source link: https://blog.jiahonzheng.com/post/git-mirror-repository/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Gitea 与 GitHub 仓库同步

· 2022-01-19 · # Git

之前弄毕设和外包项目时,使用了自建的 Gitea 来托管项目代码。出于存储安全的考虑,现打算将上面的仓库数据迁移至 GitHub

To maintain a mirror of a repository without forking it, you can run a special clone command, then mirror-push to the new repository.

查阅了相关资料,找到了 GitHub 官方对镜像仓库的说明。如果我们能够将 GitHub 作为 Gitea 的镜像仓库,就可以实现 Gitea 仓库向 GitHub 的迁移。

实现镜像仓库有两个重要的步骤:

  • 创建仓库的裸克隆 git clone --bare
  • 推送裸仓库镜像 git push --mirror

Git 仓库,其实有两种形式,一种我称为客户端仓库,另一种是服务端仓库(裸仓库)。通常,我们接触较多的其实是客户端仓库,因为我们执行克隆时,并没有带上 --bare 参数。

202201190254490.png

如上图所示,我们克隆了 ocr_cloud/server 的客户端仓库,其目录结构与我们开发时的目录结构是一致的。

而下图是 ocr_cloud/server 的服务端仓库,可以看到其目录结构与客户端仓库有很大的区别,这些目录结构是为 git 服务的,如 HEAD 文件指明当前的 ref 信息,branches 目录存放着分支信息等。

202201190254345.png

镜像仓库的实质就是将服务端仓库的信息从一个站点,拷贝到另外一个站点,这也就是第二个步骤做的事情,通过 git push --mirror 实现拷贝操作。

有了以上的知识,编写迁移脚本就很轻松啦。

#!/bin/bash

# 配置当前仓库
[email protected]
FROM_OWNER=ocr_cloud
FROM_REPO=server

# 配置镜像仓库
[email protected]
TO_OWNER=Jiahonzheng
TO_REPO=$FROM_OWNER.$FROM_REPO

# 克隆裸仓库
git clone --bare $FROM_HOST:$FROM_OWNER/$FROM_REPO.git
cd $FROM_REPO.git

# 推送镜像
git push --mirror $TO_HOST:$TO_OWNER/$TO_REPO.git

cd ..
rm -rf $FROM_REPO.git

在脚本中,我将镜像仓库名称定义为 [原仓库所有者].[原仓库名称] ,降低了仓库名称的冲突,并一定程度提高了名称的可读性。

  • 当前仓库:https://gitea.own.com/ocr_cloud/server
  • 镜像仓库:https://github.com/Jiahonzheng/ocr_cloud.server

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK