原文地址:https://alphahinex.github.io/2023/02/26/mirror-git-repo-and-keep-sync/
description: "How to mirror a git repo and keep sync"
date: 2023.02.26 10:26
categories:
- Git
tags: [Git]
keywords: git, mirror, prune, bare
有 A、B 兩個 git 倉庫,想實(shí)現(xiàn)類似主從數(shù)據(jù)庫的效果:
- A 庫作為主庫提交 Commit 記錄
- B 庫作為備份庫,同步 A 庫內(nèi)容,并在不影響 A 庫的情況下提供讀取虚婿、分析等操作
假設(shè)
TL;DR
全量鏡像,執(zhí)行一次:
rm -rf source.git
git clone --mirror http://git/repo/source.git
cd source.git
git remote add target http://another/git/sourcemirror.git
git push --mirror target
增量同步泳挥,定時執(zhí)行:
git --git-dir=/path/to/source.git fetch --prune
git --git-dir=/path/to/source.git push --mirror target
實(shí)現(xiàn)原理
實(shí)現(xiàn)原理如下圖:
大致分為三個步驟:
- 全量同步
- 增量更新
- 同步變化
其中 2/3 步可以放在一起定時執(zhí)行然痊,起到保持同步的作用,并避免了每次同步全量更新屉符,提高同步效率剧浸。
Step 1 克隆鏡像倉庫
在進(jìn)行倉庫的全量鏡像時,我們不僅希望同步某些特定分支的提交記錄矗钟,而是想完整的鏡像源倉庫的所有分支唆香、tag 等信息。
這時吨艇,通常用來更新躬它、提交代碼的 clone、pull东涡、push 操作就不太適合了冯吓。
Git 中有一個裸倉庫(bare Git reposiotry)的概念倘待,git-clone
的 Manual 中對此有如下描述:
--bare
Make a bare Git repository. That is, instead of creating
<directory> and placing the administrative files in
<directory>/.git, make the <directory> itself the $GIT_DIR. This
obviously implies the --no-checkout because there is nowhere to
check out the working tree. Also the branch heads at the remote are
copied directly to corresponding local branch heads, without
mapping them to refs/remotes/origin/. When this option is used,
neither remote-tracking branches nor the related configuration
variables are created.
即在添加了 --bare
參數(shù)進(jìn)行 git clone
時,我們得到的不再是一個包含了所有已提交文件最新版本的工作空間组贺,而是原本存在于工作空間 .git
路徑下的內(nèi)容本身凸舵。
git clone
舉個例子,在 git clone --mirror http://git/repo/source.git
之后失尖,我們得到的是一個名為 source
的文件夾啊奄,路徑下包括如下內(nèi)容:
? source git:(master) tree -a -L 2
.
├── .git
│ ├── COMMIT_EDITMSG
│ ├── HEAD
│ ├── ORIG_HEAD
│ ├── config
│ ├── description
│ ├── hooks
│ ├── index
│ ├── info
│ ├── logs
│ ├── objects
│ ├── packed-refs
│ └── refs
├── README.md
├── Test.css
├── Test.groovy
├── Test.java
├── Test.js
├── Test.less
├── Test.scss
├── Test.vue
├── test.properties
└── test.yaml
6 directories, 17 files
git clone --bare
git clone --bare http://git/repo/source.git
之后,我們得到的是一個名為 source.git
的文件夾雹仿,路徑下包括如下內(nèi)容:
? source.git git:(master) tree -L 1
.
├── FETCH_HEAD
├── HEAD
├── config
├── description
├── hooks
├── info
├── objects
├── packed-refs
└── refs
4 directories, 5 files
可以看到,克隆相同的倉庫整以,裸倉庫中不包含我們提交的文件胧辽,只包含 Git 自己生成的文件。
--mirror
vs --bare
與 --bare
參數(shù)類似公黑,git-clone
還提供了另一個參數(shù) —— --mirror
:
--mirror
Set up a mirror of the source repository. This implies --bare.
Compared to --bare, --mirror not only maps local branches of the
source to local branches of the target, it maps all refs (including
remote-tracking branches, notes etc.) and sets up a refspec
configuration such that all these refs are overwritten by a git
remote update in the target repository.
使用了 --mirror
參數(shù)邑商,相當(dāng)于同時指定了 --bare
,也會得到類似上面 $GIT_DIR
的目錄結(jié)構(gòu)凡蚜。
與 --bare
不同的是人断,通過 --mirror
參數(shù)克隆出來的裸倉庫,是可以后續(xù)增量更新的朝蜘。
所以如果只希望進(jìn)行一次性的倉庫遷移恶迈,使用哪個參數(shù)都可以;如果希望持續(xù)更新谱醇,需要使用 --mirror
暇仲。
Step 2 同步源倉庫并進(jìn)行修剪
同步
裸倉庫的更新,與普通 git 倉庫的更新也有區(qū)別副渴。
通常我們使用 git pull 拉取最新代碼奈附,而 pull 操作相當(dāng)于先執(zhí)行了 fetch 操作,再接著執(zhí)行 merge煮剧。
裸倉庫中因為沒有工作目錄斥滤,沒有辦法執(zhí)行 merge 操作,所以可以單獨(dú)使用 fetch 進(jìn)行更新勉盅,或使用 --mirror
參數(shù)文檔中提到的 git remote udpate
命令更新佑颇。
修剪
同步時我們不僅希望同步增加的內(nèi)容,也希望同步源倉庫中減少的內(nèi)容(如分支草娜、tag等)漩符,這時需要為更新命令增加 --prune
參數(shù),以修剪已不存在的內(nèi)容驱还。
-p, --prune
Before fetching, remove any remote-tracking references that no
longer exist on the remote. Tags are not subject to pruning if they
are fetched only because of the default tag auto-following or due
to a --tags option. However, if tags are fetched due to an explicit
refspec (either on the command line or in the remote configuration,
for example if the remote was cloned with the --mirror option),
then they are also subject to pruning. Supplying --prune-tags is a
shorthand for providing the tag refspec.
See the PRUNING section below for more details.
--git-dir
同步操作會定時執(zhí)行嗜暴,為方便計劃任務(wù)執(zhí)行腳本凸克,可在命令中指定 --git-dir
參數(shù),設(shè)定 git 倉庫路徑闷沥,方便腳本在任意位置執(zhí)行萎战。
--git-dir=<path>
Set the path to the repository. This can also be controlled by
setting the GIT_DIR environment variable. It can be an absolute
path or relative path to current working directory.
小結(jié)
同步并修剪,可使用下面任一方式執(zhí)行:
$ git --git-dir=/path/to/source.git fetch --prune
或
$ git --git-dir=/path/to/source.git remote update origin --prune
Step 3 推送變更
克隆鏡像倉庫舆逃,或更新變更之后蚂维,可以通過 push 命令將內(nèi)容推送至 B 倉庫。在鏡像倉庫內(nèi)執(zhí)行 push 時路狮,相當(dāng)于默認(rèn)指定了 --mirror
參數(shù):
--mirror
Instead of naming each ref to push, specifies that all refs under
refs/ (which includes but is not limited to refs/heads/,
refs/remotes/, and refs/tags/) be mirrored to the remote
repository. Newly created local refs will be pushed to the remote
end, locally updated refs will be force updated on the remote end,
and deleted refs will be removed from the remote end. This is the
default if the configuration option remote.<remote>.mirror is set.
故推送變更可使用
$ git --git-dir=/path/to/source.git push target
或
$ git --git-dir=/path/to/source.git push --mirror target
注意:指定了
--mirror
參數(shù)的推送相當(dāng)于強(qiáng)制推送虫啥,即使目標(biāo)倉庫中原本存在一些與源庫不一致的內(nèi)容,也會將兩個倉庫的內(nèi)容同步為一致的奄妨。 如果有分支被保護(hù)不允許強(qiáng)制提交涂籽,推送可能會失敗。需要臨時允許強(qiáng)制提交砸抛,待完成同步后评雌,再禁止強(qiáng)制提交即可。