git clone --mirror 同步桥

发布时间:2026/5/16 10:00:21

git clone --mirror 同步桥 情况一之前的 mirror 裸仓库还在如果你之前的cuda-quantum.gitmirror 裸仓库目录还保留着两步搞定cd~/cuda-quantum.git# Step 1: 从 a 仓库拉取最新内容包括新 tag、新分支、新提交# 因为当初 mirror 时 fetch URL 仍指向 a所以直接 fetch 即可gitfetch origin# Step 2: 推送到 b 仓库push URL 已指向 bgitpush--mirror原理说明git clone --mirror https://a.git创建的裸仓库有两个隐含配置$gitconfig--getremote.origin.url# fetch URL → 仍是 ahttps://a.git $gitconfig--getremote.origin.pushurl# push URL → 你改成了 bssh://b.git $gitconfig--getremote.origin.mirror# mirror 模式开启true所以git fetch origin从a 拉取git push --mirror往b 推送天然就是增量同步。情况二mirror 裸仓库已被删除如果之前的裸仓库已经删了不要重新 clone --mirror否则会把 Bitbucket 的refs/pull-requests/*又拉回来导致推送失败。正确做法是创建一个新的裸仓库只 fetch 分支和标签# 创建新的裸仓库gitclone--barehttps://a.git project.gitcdproject.git# 配置推送到 bgitremote set-url--pushorigin ssh://b.git# 推送所有分支和标签跳过 Bitbucket 内部保留的 refsgitpush originrefs/heads/*:refs/heads/*gitpush originrefs/tags/*:refs/tags/*如果之前的裸仓库已经推送成功过一次现在只剩增量问题推荐用情况一保留裸仓库作为长期同步桥。情况三只想同步新 tag不碰已有内容如果 b 仓库已经有大部分内容只想把 a 的新 tag 同步过去cd~/cuda-quantum.git# 获取 a 的所有新内容gitfetch origin# 只推送新 taggitpush origin--tags# 或推送所有分支只推送 b 上没有的gitpush originrefs/heads/*:refs/heads/*长期维护建一个简单的同步脚本如果 a 和 b 两个仓库需要长期保持同步建议保留那个 mirror 裸仓库并写一个定时脚本#!/bin/bash# ~/sync-mirror.shREPO_DIR$HOME/cuda-quantum.gitcd$REPO_DIR||exit1echoFetching from source (a)...gitfetch originechoPushing to mirror (b)...# 排除 Bitbucket 内部保留的命名空间gitpush originrefs/heads/*:refs/heads/*gitpush originrefs/tags/*:refs/tags/*echoDone.加 crontab 每周自动同步crontab-e# 添加02* *1/home/eili/sync-mirror.sh/home/eili/sync-mirror.log21关键命令速查需求命令从 a 增量拉取git fetch origin在 mirror 裸仓库中执行全量推送到 bgit push --mirror只推送分支标签安全git push origin refs/heads/*:refs/heads/* git push origin --tags查看 fetch/push URLgit remote -v查看新 taggit ls-remote --tags origin | tail -20

相关新闻