6

同步Git库的一个remote到另一个

 3 years ago
source link: https://note.qidong.name/2017/12/git-push-from-remote-to-remote/
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.

同步Git库的一个remote到另一个

2017-12-19 10:23:09 +08  字数:793  标签: Git

有时候有这样的需求: 一个Git库是在某个位置开发、维护的,比如国外的GitHub,但是又希望同步更新到另外一个位置,比如国内的Gitee。 这时,就需要对Git库进行全量同步。

本文以android.googlesource.com上的platform/test/vts/为例,介绍如何进行全量同步。

Git全量同步

如果在开发状态,把一个commit同时push到两个remote,这并不是很难。 难的是,把一个remote上的所有commit、branch、tag,都推送到另一个remote上。

所幸,Git其实是支持通配符进行匹配操作的。

clone一个remote

git clone https://android.googlesource.com/platform/test/vts/

clone之后,本地默认是master分支,也仅有master。 代码的完整信息,其实已经下载下来了,不过都在.git/refs/remotes/origin/,不在.git/refs/heads/下。

新增一个remote

当然,新增之前,需要在Gitee(或其它要同步位置)上创建这个项目。

git remote add gitee [email protected]:yanqd0/vts.git

直接进行git push gitee,只是push了master,达不到目的。

从一个remote推送到另一个

git push gitee refs/remotes/origin/*:refs/heads/* --tags

这样就通过*,对所有branch、tag进行了push。

如果是手工操作,为了避免麻烦,可以修改.git/config配置。

[remote "gitee"]
	url = [email protected]:yanqd0/vts.git
	fetch = +refs/heads/*:refs/remotes/gitee/*
	push = refs/remotes/origin/*:refs/heads/*

在对应的remote,新增push那一行,可以简化push命令。

git push gitee --tags

定时同步

在一台服务器上,对本例来说应该是境外的服务器上,配置定时任务。 在初始化后,通过以下两行命令,即可完成同步。

git fetch origin
git push gitee --tags

如果可以接受每次都全量下载,并且任务执行完后需要清理缓存,可以用以下脚本。

git clone https://android.googlesource.com/platform/test/vts/
cd vts
git remote add gitee [email protected]:yanqd0/vts.git
git push gitee refs/remotes/origin/*:refs/heads/* --tags
cd ..
rm -rf vts

Gitee的同步功能

如果只是从一些国内可以正常访问的位置,同步到Gitee,可以不用这么麻烦。 Gitee自带一个同步功能,在创建项目时选填即可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK