鏡像 Git 倉庫摹恨,并保持同步

原文地址: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)原理如下圖:

大致分為三個步驟:

  1. 全量同步
  2. 增量更新
  3. 同步變化

其中 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)制提交即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末直焙,一起剝皮案震驚了整個濱河市景东,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奔誓,老刑警劉巖斤吐,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異厨喂,居然都是意外死亡曲初,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門杯聚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來臼婆,“玉大人,你說我怎么就攤上這事幌绍“涔樱” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵傀广,是天一觀的道長颁独。 經(jīng)常有香客問我,道長伪冰,這世上最難降的妖魔是什么誓酒? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上靠柑,老公的妹妹穿的比我還像新娘寨辩。我一直安慰自己,他們只是感情好歼冰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布靡狞。 她就那樣靜靜地躺著,像睡著了一般隔嫡。 火紅的嫁衣襯著肌膚如雪甸怕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天腮恩,我揣著相機(jī)與錄音梢杭,去河邊找鬼。 笑死秸滴,一個胖子當(dāng)著我的面吹牛武契,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播缸榛,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼吝羞,長吁一口氣:“原來是場噩夢啊……” “哼兰伤!你這毒婦竟也來了内颗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤敦腔,失蹤者是張志新(化名)和其女友劉穎均澳,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體符衔,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡找前,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了判族。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片躺盛。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖形帮,靈堂內(nèi)的尸體忽然破棺而出槽惫,到底是詐尸還是另有隱情,我是刑警寧澤辩撑,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布界斜,位于F島的核電站,受9級特大地震影響合冀,放射性物質(zhì)發(fā)生泄漏各薇。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一君躺、第九天 我趴在偏房一處隱蔽的房頂上張望峭判。 院中可真熱鬧开缎,春花似錦、人聲如沸朝抖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽治宣。三九已至急侥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間侮邀,已是汗流浹背坏怪。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绊茧,地道東北人铝宵。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像华畏,于是被迫代替她去往敵國和親鹏秋。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容