[{"data":1,"prerenderedAt":876},["ShallowReactive",2],{"\u002Farticle\u002Fdetail\u002Fgithub-gitee-sync":3,"\u002Farticle\u002Fdetail\u002Fgithub-gitee-sync-surround":865},{"id":4,"title":5,"articleId":6,"articleThumb":7,"body":8,"categoryId":328,"categoryInfo":828,"createDate":832,"description":833,"excerpt":834,"extension":839,"isHidden":840,"isHot":841,"isRecommend":840,"isTop":840,"meta":842,"navigation":841,"path":843,"rawbody":844,"readingTime":427,"seo":845,"sitemap":846,"stem":854,"tagIds":855,"tagInfo":856,"updateDate":862,"wordCount":863,"__hash__":864},"blog\u002Fblog\u002F2.github仓库代码和gitee仓库代码双向自动同步方案.md","github仓库代码和gitee仓库代码双向自动同步方案","github-gitee-sync","https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20260327203348205.webp",{"type":9,"value":10,"toc":812},"minimark",[11,15,18,21,26,38,50,56,69,75,82,87,90,99,122,133,136,140,143,152,197,200,203,206,217,225,229,236,240,246,250,256,260,263,280,467,470,474,483,494,498,501,506,515,520,527,532,542,688,695,708,711,787,790,794,797,800,804,808],[12,13,14],"p",{},"Github同步代码至Gitee或者是从Gitee更新代码，都需要借助Github Actions功能。GitHub Actions‌ 是 GitHub 提供的持续集成与持续交付（CI\u002FCD）平台，主要用于自动化软件开发流程中的构建、测试和部署任务。通过yml格式文件配置workflow实现自动化操作，可以实现一次提交，多仓库代码同步。目前Github Actions在实现本地代码提交Github仓库时，同步代码到Gitee，可以自动触发workflow执行，实现自动同步功能。反过来，当Gitee代码库存在更新，Github从Gitee同步更新时，借助Github Actions无法实现自动，只能借助定时任务，定时从Gitee同步代码。",[12,16,17],{},"相对于Github同步方案而言，Gitee同步代码至Github或者从Github更新代码非常简单。借助Gitee提供的强大的仓库镜像功能，只需要在Gitee的源仓库上进行简单配置就可以实现。",[12,19,20],{},"下面介绍Github和Gitee双向同步的具体实现步骤。",[22,23,25],"h2",{"id":24},"_1-github自动同步gitee","1 Github自动同步Gitee",[12,27,28,29,37],{},"Github自动同步Gitee是指在本地提交代码至Github远程仓库时，同步将代码更新至Gitee仓库。目前通过Github Actions 实现Github仓库同步到gitee的Actions有很多，本文主要讲述如何使用【",[30,31,36],"a",{"href":32,"rel":33,"target":35},"https:\u002F\u002Fgithub.com\u002Fwearerequired\u002Fgit-mirror-action",[34],"nofollow","_blank","git-mirror-action","】镜像来实现同步操作。",[12,39,40,44,45,49],{},[41,42,36],"code",{"color":43},"primary"," 是一个用于自动同步 GitHub 和 Gitee（或其他 Git 平台）仓库的 GitHub Action，通过配置工作流（workflow）实现自动化部署。github仓库地址为：【",[30,46,48],{"href":32,"rel":47,"target":35},[34],"wearerequired\u002Fgit-mirror-action@master","】",[12,51,52],{},[53,54,55],"strong",{},"核心功能如下：",[57,58,59,63,66],"ul",{},[60,61,62],"li",{},"双向同步：支持将 GitHub 仓库同步到 Gitee 或其他平台，也可实现反向同步。",[60,64,65],{},"‌灵活配置‌：通过 .yml 文件定义同步规则，支持源仓库与目标仓库的地址映射。",[60,67,68],{},"自动化部署‌：结合 GitHub Actions 工作流，可在代码更新时自动触发同步操作。",[12,70,71,74],{},[53,72,73],{},"同步的原理","：利用 SSH 公私钥配对的方式拉取 Github 仓库的代码并推送到 Gitee 仓库中。",[12,76,77,78],{},"详细配置参考：",[30,79,81],{"href":32,"rel":80},[34],"github源码仓库文档",[83,84,86],"h3",{"id":85},"_11-创建gitee目标仓库","1.1 创建Gitee目标仓库",[12,88,89],{},"在实现github仓库同步到gitee仓库之前，首先需要在gitee初始化一个仓库，作为同步的目标仓库。可以直接创建一个空仓库，或者直接通过gitee自带的导入github仓库代码功能。具体导入github仓库步骤如下：",[12,91,92,93,98],{},"① 登陆[",[30,94,97],{"href":95,"rel":96,"target":35},"https:\u002F\u002Fgitee.com\u002Flogin",[34],"gitee","] -> 右上角加号 -> 从github\u002Fgitlab导入仓库。进入页面后，可直接通过url输入，也可以通过github授权后，批量选择导入的仓库。",[100,101,104],"callout",{"color":102,"icon":103},"warning","material-symbols:info-outline",[12,105,106,107,110,111,114,115,49,119,121],{},"温馨提示",[108,109],"br",{},"\n1 从url导入私有仓库时，需要填写仓库的账户和个人令牌。此处的令牌并非密码(github现在不建议直接账户密码认证)，而是GitHub 的「",[53,112,113],{},"Personal access token","」。点击前往设置【",[30,116,113],{"href":117,"rel":118,"target":35},"https:\u002F\u002Fgithub.com\u002Fsettings\u002Ftokens",[34],[108,120],{},"\n2 从github导入仓库，需要github授权才行，授权后可以选择需要导入的仓库。",[12,123,124,125,129,130,132],{},"② 仓库导入成功后，进入仓库，仓库名称后面会显示一个 ",[126,127],"icon",{"name":128},"material-symbols-light:refresh-rounded"," 符号，点击便可手动同步github仓库代码至gitee。私有仓库同步时，需要输入账号和个人令牌(github的",[53,131,113],{},")",[12,134,135],{},"至此，github仓库同步gitee仓库前期准备工作已准备完毕，下面开始介绍如何配置github自动同步代码至gitee。",[83,137,139],{"id":138},"_12-创建ssh公私钥","1.2 创建SSH公私钥",[12,141,142],{},"SSH密钥对由公钥和私钥组成，公钥用于加密数据，私钥用于解密数据，通过非对称加密确保通信安全。使用 SSH 协议可以连接远程服务器和服务并向它们验证。 利用 SSH 密钥可以连接到 GitHub，而无需在每次访问时都提供用户名和 personal access token。GitHub通过公钥加密信息，私钥用于解密，从而确认用户身份。",[12,144,145,146,148,149,151],{},"①在本地机器上打开终端应用程序",[108,147],{},"\n②生成SSH密钥对",[108,150],{},"\n使用ssh-keygen命令来生成密钥对。您可以通过以下命令开始：",[153,154,159],"pre",{"className":155,"code":156,"language":157,"meta":158,"style":158},"language-shell shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","ssh-keygen -t rsa  -b 4096 -C \"youremail@example.com\"\n","shell","",[41,160,161],{"__ignoreMap":158},[162,163,166,170,174,177,180,184,187,191,194],"span",{"class":164,"line":165},"line",1,[162,167,169],{"class":168},"sBMFI","ssh-keygen",[162,171,173],{"class":172},"sfazB"," -t",[162,175,176],{"class":172}," rsa",[162,178,179],{"class":172},"  -b",[162,181,183],{"class":182},"sbssI"," 4096",[162,185,186],{"class":172}," -C",[162,188,190],{"class":189},"sMK4o"," \"",[162,192,193],{"class":172},"youremail@example.com",[162,195,196],{"class":189},"\"\n",[12,198,199],{},"这里，-t rsa指定了密钥的类型为RSA，-b 4096指定了密钥的位长度为4096位。RSA密钥的长度可以是1024位、2048位或4096位，推荐使用2048位或更高的安全性。-C（Comment）是 ssh-keygen 命令的一个选项，用于为生成的 SSH 密钥对添加注释。注释内容可以是任意文本，但通常推荐使用邮箱地址或密钥用途描述。注释仅保存在公钥文件中，私钥文件不包含此信息。",[12,201,202],{},"执行上述命令后，系统会询问您希望将密钥存储在哪个文件中，默认是~\u002F.ssh\u002Fid_rsa（私钥）和~\u002F.ssh\u002Fid_rsa.pub（公钥）。您可以按回车接受默认设置，或者输入其他路径来存储密钥文件。",[12,204,205],{},"接下来，系统会询问您是否要为密钥设置密码（passphrase）。强烈建议设置密码，因为即使私钥文件被保护，知道密码仍然可以访问您的服务器。如果您不想设置密码，可以按回车跳过此步骤。",[12,207,208,209,212,213,216],{},"如果连续三次回车，生成的秘钥对存储默认位置。",[41,210,211],{"color":43},"id_rsa"," 为私钥，",[41,214,215],{"color":43},"id_rsa.pub","为公钥。如果提示：already exists（已经存在），则可以到电脑位置：C:\\Users\\电脑账号名\\ .ssh 直接使用。",[12,218,219,220,49],{},"具体图文参考：【",[30,221,224],{"href":222,"rel":223,"target":35},"https:\u002F\u002Fgitee.com\u002Fhelp\u002Farticles\u002F4181#article-header0",[34],"生成\u002F添加SSH公钥",[83,226,228],{"id":227},"_13-github配置ssh私钥","1.3 GitHub配置SSH私钥",[12,230,231,232],{},"打开Github需要同步的项目，点击Settings->Secrets->Actions，设置私钥名称为：GITEE_RSA_PRIVATE_KEY，值为：上面生成SSH的私钥，即id_rsa 文件的内容\n",[233,234],"img",{"alt":158,"src":235},"https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910125740784.webp",[83,237,239],{"id":238},"_14-github配置ssh公钥","1.4 GitHub配置SSH公钥",[12,241,242,243],{},"在Github点击右上角用户图像，点击Settings->SSH and GPG keys->New SSH key，名称为：GITEE_RSA_PUBLIC_KEY，值为：上面生成SSH的公钥，即id_rsa_pub文件内容\n",[233,244],{"alt":158,"src":245},"https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910130224827.webp",[83,247,249],{"id":248},"_15-gitee配置ssh公钥","1.5 Gitee配置SSH公钥",[12,251,252,253],{},"在Gitee 设置->安全设置->SSH公钥，标题为：GITEE_RSA_PUBLIC_KEY，值为：上面生成SSH的公钥，即id_rsa_pub文件内容\n",[233,254],{"alt":158,"src":255},"https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910130741176.webp",[83,257,259],{"id":258},"_16-创建github-workflow","1.6 创建Github workflow",[12,261,262],{},"github创建workflow有2种方式，第一种直接在github项目中，点击actions创建新的workflow，第二种就是将项目clone本地，进行配置。二者大同小异，都是配置相应的设置。",[12,264,265,266,269,272,275,276,279],{},"①第一种：点击actions创建新的workflow\n",[233,267],{"alt":158,"src":268},"https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910131433758.webp",[233,270],{"alt":158,"src":271},"https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910131520866.webp",[233,273],{"alt":158,"src":274},"https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910131747275.webp","\n②第二种：将项目clone到本地，用vscode打开，根目录下创建文件",[41,277,278],{"color":43},".github\\workflows\\sync-to-gitee.yml","，内容如下：",[153,281,286],{"className":282,"code":283,"filename":284,"language":285,"meta":158,"style":158},"language-yml shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","name: Sync to Gitee\non:\n    push:\n        branches:\n            # 或者你想要同步的分支\n            - main\njobs:\n    sync:\n        runs-on: ubuntu-latest\n        steps:\n            - name: Sync to Gitee\n              uses: wearerequired\u002Fgit-mirror-action@master\n              env:\n                  # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY\n                  SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}\n              with:\n                  # 注意替换为你的 GitHub 源仓库地址\n                  source-repo: git@github.com:AnyFork\u002Fanyfork-docs-client.git\n                  # 注意替换为你的 Gitee 目标仓库地址\n                  destination-repo: git@gitee.com:AnyFork\u002Fanyfork-docs-client.git\n","sync-to-gitee.yml","yml",[41,287,288,300,310,318,326,333,342,350,358,369,377,389,400,408,414,425,433,439,450,456],{"__ignoreMap":158},[162,289,290,294,297],{"class":164,"line":165},[162,291,293],{"class":292},"swJcz","name",[162,295,296],{"class":189},":",[162,298,299],{"class":172}," Sync to Gitee\n",[162,301,303,307],{"class":164,"line":302},2,[162,304,306],{"class":305},"sfNiH","on",[162,308,309],{"class":189},":\n",[162,311,313,316],{"class":164,"line":312},3,[162,314,315],{"class":292},"    push",[162,317,309],{"class":189},[162,319,321,324],{"class":164,"line":320},4,[162,322,323],{"class":292},"        branches",[162,325,309],{"class":189},[162,327,329],{"class":164,"line":328},5,[162,330,332],{"class":331},"sHwdD","            # 或者你想要同步的分支\n",[162,334,336,339],{"class":164,"line":335},6,[162,337,338],{"class":189},"            -",[162,340,341],{"class":172}," main\n",[162,343,345,348],{"class":164,"line":344},7,[162,346,347],{"class":292},"jobs",[162,349,309],{"class":189},[162,351,353,356],{"class":164,"line":352},8,[162,354,355],{"class":292},"    sync",[162,357,309],{"class":189},[162,359,361,364,366],{"class":164,"line":360},9,[162,362,363],{"class":292},"        runs-on",[162,365,296],{"class":189},[162,367,368],{"class":172}," ubuntu-latest\n",[162,370,372,375],{"class":164,"line":371},10,[162,373,374],{"class":292},"        steps",[162,376,309],{"class":189},[162,378,380,382,385,387],{"class":164,"line":379},11,[162,381,338],{"class":189},[162,383,384],{"class":292}," name",[162,386,296],{"class":189},[162,388,299],{"class":172},[162,390,392,395,397],{"class":164,"line":391},12,[162,393,394],{"class":292},"              uses",[162,396,296],{"class":189},[162,398,399],{"class":172}," wearerequired\u002Fgit-mirror-action@master\n",[162,401,403,406],{"class":164,"line":402},13,[162,404,405],{"class":292},"              env",[162,407,309],{"class":189},[162,409,411],{"class":164,"line":410},14,[162,412,413],{"class":331},"                  # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY\n",[162,415,417,420,422],{"class":164,"line":416},15,[162,418,419],{"class":292},"                  SSH_PRIVATE_KEY",[162,421,296],{"class":189},[162,423,424],{"class":172}," ${{ secrets.GITEE_RSA_PRIVATE_KEY }}\n",[162,426,428,431],{"class":164,"line":427},16,[162,429,430],{"class":292},"              with",[162,432,309],{"class":189},[162,434,436],{"class":164,"line":435},17,[162,437,438],{"class":331},"                  # 注意替换为你的 GitHub 源仓库地址\n",[162,440,442,445,447],{"class":164,"line":441},18,[162,443,444],{"class":292},"                  source-repo",[162,446,296],{"class":189},[162,448,449],{"class":172}," git@github.com:AnyFork\u002Fanyfork-docs-client.git\n",[162,451,453],{"class":164,"line":452},19,[162,454,455],{"class":331},"                  # 注意替换为你的 Gitee 目标仓库地址\n",[162,457,459,462,464],{"class":164,"line":458},20,[162,460,461],{"class":292},"                  destination-repo",[162,463,296],{"class":189},[162,465,466],{"class":172}," git@gitee.com:AnyFork\u002Fanyfork-docs-client.git\n",[12,468,469],{},"至此，github workflow配置完毕。以后提交代码至github仓库时，便会触发workflow自动执行，自动将github仓库代码同步至gitee仓库中。",[22,471,473],{"id":472},"_2-github从gitee同步更新","2 Github从Gitee同步更新",[12,475,476,477,482],{},"Github仓库从Gitee仓库同步代码，也是借助Github Actions功能进行实现，能够实现此功能的Actions较多，本文还是采用\n【",[30,478,481],{"href":479,"rel":480,"target":35},"https:\u002F\u002Fgithub.com\u002FYikun\u002Fhub-mirror-action",[34],"Yikun\u002Fgitee-mirror-action","】进行实现。",[12,484,485,486,489,490,493],{},"由于Github Actions在Gitee代码仓库更新时，无法自动感知gitee仓库发生更新，从而不能自动触发workflow的执行，因此形成了2种不同的同步方式：",[53,487,488],{},"webhooks实时同步方式","和",[53,491,492],{},"定时延迟同步方式","。下面分别介绍这2种同步方式的具体实现步骤。",[83,495,497],{"id":496},"_21-定时延迟同步方式","2.1 定时延迟同步方式",[12,499,500],{},"定时延迟同步方式是最简单的一种方式，虽然存在延迟，但大部分情况下对代码同步的实时性要求并不高。可以根据实际需求，调整定时任务频率，增加同步次数。",[12,502,503],{},[53,504,505],{},"1 创建Github目标仓库",[12,507,508,509,514],{},"在实现从Gitee同步代码至目标仓库Github之前，需要在Github上创建一个空的仓库作为同步的目标仓库。登陆【",[30,510,513],{"href":511,"rel":512,"target":35},"https:\u002F\u002Fgithub.com\u002F",[34],"gihub","】->右上角加号->创建仓库，私有或者公有仓库均可。",[12,516,517],{},[53,518,519],{},"2 目标仓库配置SSH私钥",[12,521,522,523,49],{},"github目标仓库创建好后，需要配置仓库SSH私钥，具体步骤参考：【",[30,524,526],{"href":525},"#_13-github%E9%85%8D%E7%BD%AEssh%E7%A7%81%E9%92%A5","1.3 github配置ssh私钥",[12,528,529],{},[53,530,531],{},"3 目标仓库创建Github workflow",[12,533,534,535,538,539,279],{},"在【",[30,536,259],{"href":537},"#_16-%E5%88%9B%E5%BB%BAgithub-workflow","】已经讲述了如何创建Github workflow，此处不在赘述。下面采用第二种方式进行配置。将项目clone到本地，用vscode打开，根目录下创建文件",[41,540,541],{"color":43},".github\\workflows\\sync-to-github.yml",[153,543,546],{"className":282,"code":544,"filename":545,"language":285,"meta":158,"style":158},"name: Sync to Github\non:\n   schedule:\n    # 每个小时10-59分钟之间，从10分钟开始，每5分钟执行一次\n    - cron:  '10\u002F5 * * * *'\njobs:\n    sync:\n        runs-on: ubuntu-latest\n        steps:\n            - name: Sync to Github\n              uses: wearerequired\u002Fgit-mirror-action@master\n              env:\n                  # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY\n                  SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}\n              with:\n                  # 注意替换为你的 Gitee源仓库地址\n                  source-repo: git@gitee.com:AnyFork\u002Fanyfork-docs-client.git\n                  # 注意替换为你的 Github 目标仓库地址\n                  destination-repo: git@github.com:AnyFork\u002Fanyfork-docs-client.git\n","sync-to-github.yml",[41,547,548,557,563,570,575,594,600,606,614,620,630,638,644,648,656,662,667,675,680],{"__ignoreMap":158},[162,549,550,552,554],{"class":164,"line":165},[162,551,293],{"class":292},[162,553,296],{"class":189},[162,555,556],{"class":172}," Sync to Github\n",[162,558,559,561],{"class":164,"line":302},[162,560,306],{"class":305},[162,562,309],{"class":189},[162,564,565,568],{"class":164,"line":312},[162,566,567],{"class":292},"   schedule",[162,569,309],{"class":189},[162,571,572],{"class":164,"line":320},[162,573,574],{"class":331},"    # 每个小时10-59分钟之间，从10分钟开始，每5分钟执行一次\n",[162,576,577,580,583,585,588,591],{"class":164,"line":328},[162,578,579],{"class":189},"    -",[162,581,582],{"class":292}," cron",[162,584,296],{"class":189},[162,586,587],{"class":189},"  '",[162,589,590],{"class":172},"10\u002F5 * * * *",[162,592,593],{"class":189},"'\n",[162,595,596,598],{"class":164,"line":335},[162,597,347],{"class":292},[162,599,309],{"class":189},[162,601,602,604],{"class":164,"line":344},[162,603,355],{"class":292},[162,605,309],{"class":189},[162,607,608,610,612],{"class":164,"line":352},[162,609,363],{"class":292},[162,611,296],{"class":189},[162,613,368],{"class":172},[162,615,616,618],{"class":164,"line":360},[162,617,374],{"class":292},[162,619,309],{"class":189},[162,621,622,624,626,628],{"class":164,"line":371},[162,623,338],{"class":189},[162,625,384],{"class":292},[162,627,296],{"class":189},[162,629,556],{"class":172},[162,631,632,634,636],{"class":164,"line":379},[162,633,394],{"class":292},[162,635,296],{"class":189},[162,637,399],{"class":172},[162,639,640,642],{"class":164,"line":391},[162,641,405],{"class":292},[162,643,309],{"class":189},[162,645,646],{"class":164,"line":402},[162,647,413],{"class":331},[162,649,650,652,654],{"class":164,"line":410},[162,651,419],{"class":292},[162,653,296],{"class":189},[162,655,424],{"class":172},[162,657,658,660],{"class":164,"line":416},[162,659,430],{"class":292},[162,661,309],{"class":189},[162,663,664],{"class":164,"line":427},[162,665,666],{"class":331},"                  # 注意替换为你的 Gitee源仓库地址\n",[162,668,669,671,673],{"class":164,"line":435},[162,670,444],{"class":292},[162,672,296],{"class":189},[162,674,466],{"class":172},[162,676,677],{"class":164,"line":441},[162,678,679],{"class":331},"                  # 注意替换为你的 Github 目标仓库地址\n",[162,681,682,684,686],{"class":164,"line":452},[162,683,461],{"class":292},[162,685,296],{"class":189},[162,687,449],{"class":172},[12,689,690,691,694],{},"上面配置了",[41,692,693],{"color":43},"on:schedule:cron","定时任务，时间到了定时任务设置的时间，便会触发workflow执行。",[100,696,697],{"color":102,"icon":103},[12,698,106,699,701,702,704,705,707],{},[108,700],{},"\n1 设定的 schedule 是UCT时间，比如设置的08:00（也就是北京时间的16:00），但实际运行的时间为北京时间17:00 至 17：20 不等。",[108,703],{},"\n2 Schedule在 GitHub 操作工作流运行的高负载期间，事件可能会延迟。高负载时间包括每小时开始。为了减少延迟的可能性，请安排您的工作流在不同的时间运行。实测延迟的时间为几十分钟，或者超过一个小时，甚至在某种极端情况下，将不会执行。",[108,706],{},"\n3 Schedule 设置的 cron 时刻，仅仅是工作流进行计划排队的时刻，而不是准确的运行时刻。",[12,709,710],{},"GitHub Actions 默认使用 UTC 时间。如果你的工作流程应该在特定本地时间执行，你可以在 YAML 文件中指定时区：",[153,712,714],{"className":282,"code":713,"language":285,"meta":158,"style":158},"jobs:\n  job1:\n    runs-on: ubuntu-latest\n    steps:\n    - name: Set Timezone\n      uses: szenius\u002Fset-timezone@v1.0\n      with:\n        timezoneLinux: \"Asia\u002FShanghai\"\n\n",[41,715,716,722,729,738,745,756,766,773],{"__ignoreMap":158},[162,717,718,720],{"class":164,"line":165},[162,719,347],{"class":292},[162,721,309],{"class":189},[162,723,724,727],{"class":164,"line":302},[162,725,726],{"class":292},"  job1",[162,728,309],{"class":189},[162,730,731,734,736],{"class":164,"line":312},[162,732,733],{"class":292},"    runs-on",[162,735,296],{"class":189},[162,737,368],{"class":172},[162,739,740,743],{"class":164,"line":320},[162,741,742],{"class":292},"    steps",[162,744,309],{"class":189},[162,746,747,749,751,753],{"class":164,"line":328},[162,748,579],{"class":189},[162,750,384],{"class":292},[162,752,296],{"class":189},[162,754,755],{"class":172}," Set Timezone\n",[162,757,758,761,763],{"class":164,"line":335},[162,759,760],{"class":292},"      uses",[162,762,296],{"class":189},[162,764,765],{"class":172}," szenius\u002Fset-timezone@v1.0\n",[162,767,768,771],{"class":164,"line":344},[162,769,770],{"class":292},"      with",[162,772,309],{"class":189},[162,774,775,778,780,782,785],{"class":164,"line":352},[162,776,777],{"class":292},"        timezoneLinux",[162,779,296],{"class":189},[162,781,190],{"class":189},[162,783,784],{"class":172},"Asia\u002FShanghai",[162,786,196],{"class":189},[12,788,789],{},"对于一些不需要实时进行同步的需求，可以采用定时延迟同步方案，方便配置也简单。",[83,791,793],{"id":792},"_22-实时同步方式","2.2 实时同步方式",[12,795,796],{},"Webhook是一种事件驱动的通知机制，当发生特定事件（如代码推送、拉取请求创建等）时，会自动向指定的外部服务器发送HTTP POST请求（通常包含事件数据）。这种机制替代了轮询API的实时通知方式，允许开发者在事件发生时立即触发外部操作（如部署、测试或发送通知）。",[12,798,799],{},"Github和Gitee都支持Webhook功能，因此我们可以借助这一功能，在gitee代码发生提交时，触发gitee配置的自定义webhook，webhook通过调用github Api触发github actions自动执行，从而实现实时同步方式。",[22,801,803],{"id":802},"_3-gitee自动同步github","3 Gitee自动同步Github",[22,805,807],{"id":806},"_4-gitee从github同步更新","4 Gitee从Github同步更新",[809,810,811],"style",{},"html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sfNiH, html code.shiki .sfNiH{--shiki-light:#FF5370;--shiki-default:#FF9CAC;--shiki-dark:#FF9CAC}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}",{"title":158,"searchDepth":312,"depth":312,"links":813},[814,822,826,827],{"id":24,"depth":302,"text":25,"children":815},[816,817,818,819,820,821],{"id":85,"depth":312,"text":86},{"id":138,"depth":312,"text":139},{"id":227,"depth":312,"text":228},{"id":238,"depth":312,"text":239},{"id":248,"depth":312,"text":249},{"id":258,"depth":312,"text":259},{"id":472,"depth":302,"text":473,"children":823},[824,825],{"id":496,"depth":312,"text":497},{"id":792,"depth":312,"text":793},{"id":802,"depth":302,"text":803},{"id":806,"depth":302,"text":807},{"name":829,"symbol":830,"icon":831,"id":328},"仓库同步","repository","material-symbols-light:sync-desktop-outline","2025-09-09 21:55:35","‌日常工作中，可能会遇到github代码仓库需要自动同步代码至gitee仓库，或者gitee仓库代码同步至github仓库的应用场景，针对这个应用场景，下面便归纳一下github和gitee仓库实现双向自动同步方案和步骤。",{"type":9,"value":835},[836],[12,837,838],{},"到目前为止，想要实现在本地提交代码至Github仓库时，同时更新至Gitee仓库是可行的。相反，想要在本地提交代码至Gitee仓库时，同时更新至Github仓库也可以实现的。无论我们使用Github还是Gitee任意一个远程仓库，在代码提交时，头可以自动同步到另一个远程仓库，即所谓的双向自动同步(可推送至远端仓库，亦可从远端仓库同步更新)。当然，日常开发中，按照个人喜好，我们只需要选择一种即可。","md",false,true,{},"\u002Fblog\u002Fgithubgitee","---\ntitle: github仓库代码和gitee仓库代码双向自动同步方案\ndescription: ‌日常工作中，可能会遇到github代码仓库需要自动同步代码至gitee仓库，或者gitee仓库代码同步至github仓库的应用场景，针对这个应用场景，下面便归纳一下github和gitee仓库实现双向自动同步方案和步骤。\narticleId: github-gitee-sync\ncreateDate: \"2025-09-09 21:55:35\"\nupdateDate: \"2026-03-12 10:02:23\"\narticleThumb: https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20260327203348205.webp\ntagIds: \n    - 2\n    - 3\ncategoryId: 5\nisTop: false\nisRecommend: false\nisHot: true\nisHidden: false\nnavigation: true\n---\n\n到目前为止，想要实现在本地提交代码至Github仓库时，同时更新至Gitee仓库是可行的。相反，想要在本地提交代码至Gitee仓库时，同时更新至Github仓库也可以实现的。无论我们使用Github还是Gitee任意一个远程仓库，在代码提交时，头可以自动同步到另一个远程仓库，即所谓的双向自动同步(可推送至远端仓库，亦可从远端仓库同步更新)。当然，日常开发中，按照个人喜好，我们只需要选择一种即可。\n\u003C!--more-->\n\n\nGithub同步代码至Gitee或者是从Gitee更新代码，都需要借助Github Actions功能。GitHub Actions‌ 是 GitHub 提供的持续集成与持续交付（CI\u002FCD）平台，主要用于自动化软件开发流程中的构建、测试和部署任务。通过yml格式文件配置workflow实现自动化操作，可以实现一次提交，多仓库代码同步。目前Github Actions在实现本地代码提交Github仓库时，同步代码到Gitee，可以自动触发workflow执行，实现自动同步功能。反过来，当Gitee代码库存在更新，Github从Gitee同步更新时，借助Github Actions无法实现自动，只能借助定时任务，定时从Gitee同步代码。\n\n\n相对于Github同步方案而言，Gitee同步代码至Github或者从Github更新代码非常简单。借助Gitee提供的强大的仓库镜像功能，只需要在Gitee的源仓库上进行简单配置就可以实现。\n\n\n下面介绍Github和Gitee双向同步的具体实现步骤。\n\n\n## 1 Github自动同步Gitee\nGithub自动同步Gitee是指在本地提交代码至Github远程仓库时，同步将代码更新至Gitee仓库。目前通过Github Actions 实现Github仓库同步到gitee的Actions有很多，本文主要讲述如何使用【[git-mirror-action](https:\u002F\u002Fgithub.com\u002Fwearerequired\u002Fgit-mirror-action){target=_blank}】镜像来实现同步操作。\n\n`git-mirror-action`{color=primary} 是一个用于自动同步 GitHub 和 Gitee（或其他 Git 平台）仓库的 GitHub Action，通过配置工作流（workflow）实现自动化部署。github仓库地址为：【[wearerequired\u002Fgit-mirror-action@master](https:\u002F\u002Fgithub.com\u002Fwearerequired\u002Fgit-mirror-action){target=_blank}】\n \n**核心功能如下：**\n- 双向同步：支持将 GitHub 仓库同步到 Gitee 或其他平台，也可实现反向同步。\n- ‌灵活配置‌：通过 .yml 文件定义同步规则，支持源仓库与目标仓库的地址映射。\n- 自动化部署‌：结合 GitHub Actions 工作流，可在代码更新时自动触发同步操作。\n\n**同步的原理**：利用 SSH 公私钥配对的方式拉取 Github 仓库的代码并推送到 Gitee 仓库中。\n\n\n详细配置参考：[github源码仓库文档](https:\u002F\u002Fgithub.com\u002Fwearerequired\u002Fgit-mirror-action)\n\n\n### 1.1 创建Gitee目标仓库\n在实现github仓库同步到gitee仓库之前，首先需要在gitee初始化一个仓库，作为同步的目标仓库。可以直接创建一个空仓库，或者直接通过gitee自带的导入github仓库代码功能。具体导入github仓库步骤如下：\n\n\n① 登陆[[gitee](https:\u002F\u002Fgitee.com\u002Flogin){target=_blank}] -> 右上角加号 -> 从github\u002Fgitlab导入仓库。进入页面后，可直接通过url输入，也可以通过github授权后，批量选择导入的仓库。\n::callout{icon=\"material-symbols:info-outline\" color=\"warning\"}\n温馨提示\u003C\u002Fbr>\n1 从url导入私有仓库时，需要填写仓库的账户和个人令牌。此处的令牌并非密码(github现在不建议直接账户密码认证)，而是GitHub 的「**Personal access token**」。点击前往设置【[Personal access token](https:\u002F\u002Fgithub.com\u002Fsettings\u002Ftokens){target=_blank}】\u003C\u002Fbr>\n2 从github导入仓库，需要github授权才行，授权后可以选择需要导入的仓库。\n::\n② 仓库导入成功后，进入仓库，仓库名称后面会显示一个 :icon{name=\"material-symbols-light:refresh-rounded\"} 符号，点击便可手动同步github仓库代码至gitee。私有仓库同步时，需要输入账号和个人令牌(github的**Personal access token**)\n\n\n至此，github仓库同步gitee仓库前期准备工作已准备完毕，下面开始介绍如何配置github自动同步代码至gitee。\n\n\n### 1.2 创建SSH公私钥\nSSH密钥对由公钥和私钥组成，公钥用于加密数据，私钥用于解密数据，通过非对称加密确保通信安全。使用 SSH 协议可以连接远程服务器和服务并向它们验证。 利用 SSH 密钥可以连接到 GitHub，而无需在每次访问时都提供用户名和 personal access token。GitHub通过公钥加密信息，私钥用于解密，从而确认用户身份。\n\n\n①在本地机器上打开终端应用程序  \n②生成SSH密钥对  \n使用ssh-keygen命令来生成密钥对。您可以通过以下命令开始：\n```shell\nssh-keygen -t rsa  -b 4096 -C \"youremail@example.com\"\n```\n这里，-t rsa指定了密钥的类型为RSA，-b 4096指定了密钥的位长度为4096位。RSA密钥的长度可以是1024位、2048位或4096位，推荐使用2048位或更高的安全性。-C（Comment）是 ssh-keygen 命令的一个选项，用于为生成的 SSH 密钥对添加注释。注释内容可以是任意文本，但通常推荐使用邮箱地址或密钥用途描述。注释仅保存在公钥文件中，私钥文件不包含此信息。\n\n\n执行上述命令后，系统会询问您希望将密钥存储在哪个文件中，默认是~\u002F.ssh\u002Fid_rsa（私钥）和~\u002F.ssh\u002Fid_rsa.pub（公钥）。您可以按回车接受默认设置，或者输入其他路径来存储密钥文件。\n\n接下来，系统会询问您是否要为密钥设置密码（passphrase）。强烈建议设置密码，因为即使私钥文件被保护，知道密码仍然可以访问您的服务器。如果您不想设置密码，可以按回车跳过此步骤。\n\n\n如果连续三次回车，生成的秘钥对存储默认位置。`id_rsa`{color=primary} 为私钥，`id_rsa.pub`{color=primary}为公钥。如果提示：already exists（已经存在），则可以到电脑位置：C:\\Users\\电脑账号名\\ .ssh 直接使用。\n\n\n具体图文参考：【[生成\u002F添加SSH公钥](https:\u002F\u002Fgitee.com\u002Fhelp\u002Farticles\u002F4181#article-header0){target=_blank}】\n\n\n### 1.3 GitHub配置SSH私钥\n打开Github需要同步的项目，点击Settings->Secrets->Actions，设置私钥名称为：GITEE_RSA_PRIVATE_KEY，值为：上面生成SSH的私钥，即id_rsa 文件的内容\n![](https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910125740784.webp)\n\n\n### 1.4 GitHub配置SSH公钥\n在Github点击右上角用户图像，点击Settings->SSH and GPG keys->New SSH key，名称为：GITEE_RSA_PUBLIC_KEY，值为：上面生成SSH的公钥，即id_rsa_pub文件内容\n![](https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910130224827.webp)\n\n\n### 1.5 Gitee配置SSH公钥\n在Gitee 设置->安全设置->SSH公钥，标题为：GITEE_RSA_PUBLIC_KEY，值为：上面生成SSH的公钥，即id_rsa_pub文件内容\n![](https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910130741176.webp)\n\n\n### 1.6 创建Github workflow\ngithub创建workflow有2种方式，第一种直接在github项目中，点击actions创建新的workflow，第二种就是将项目clone本地，进行配置。二者大同小异，都是配置相应的设置。\n\n\n①第一种：点击actions创建新的workflow\n![](https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910131433758.webp)\n![](https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910131520866.webp)\n![](https:\u002F\u002Fcdn.anyfork.top\u002Fimages\u002F20250910131747275.webp)\n②第二种：将项目clone到本地，用vscode打开，根目录下创建文件`.github\\workflows\\sync-to-gitee.yml`{color=primary}，内容如下：\n```yml[sync-to-gitee.yml]\nname: Sync to Gitee\non:\n    push:\n        branches:\n            # 或者你想要同步的分支\n            - main\njobs:\n    sync:\n        runs-on: ubuntu-latest\n        steps:\n            - name: Sync to Gitee\n              uses: wearerequired\u002Fgit-mirror-action@master\n              env:\n                  # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY\n                  SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}\n              with:\n                  # 注意替换为你的 GitHub 源仓库地址\n                  source-repo: git@github.com:AnyFork\u002Fanyfork-docs-client.git\n                  # 注意替换为你的 Gitee 目标仓库地址\n                  destination-repo: git@gitee.com:AnyFork\u002Fanyfork-docs-client.git\n```\n至此，github workflow配置完毕。以后提交代码至github仓库时，便会触发workflow自动执行，自动将github仓库代码同步至gitee仓库中。\n\n\n## 2 Github从Gitee同步更新\nGithub仓库从Gitee仓库同步代码，也是借助Github Actions功能进行实现，能够实现此功能的Actions较多，本文还是采用\n【[Yikun\u002Fgitee-mirror-action](https:\u002F\u002Fgithub.com\u002FYikun\u002Fhub-mirror-action){target=_blank}】进行实现。\n\n由于Github Actions在Gitee代码仓库更新时，无法自动感知gitee仓库发生更新，从而不能自动触发workflow的执行，因此形成了2种不同的同步方式：**webhooks实时同步方式**和**定时延迟同步方式**。下面分别介绍这2种同步方式的具体实现步骤。\n\n### 2.1 定时延迟同步方式\n定时延迟同步方式是最简单的一种方式，虽然存在延迟，但大部分情况下对代码同步的实时性要求并不高。可以根据实际需求，调整定时任务频率，增加同步次数。\n\n**1 创建Github目标仓库**\n\n 在实现从Gitee同步代码至目标仓库Github之前，需要在Github上创建一个空的仓库作为同步的目标仓库。登陆【[gihub](https:\u002F\u002Fgithub.com\u002F){target=_blank}】->右上角加号->创建仓库，私有或者公有仓库均可。\n\n\n**2 目标仓库配置SSH私钥**\n\n\ngithub目标仓库创建好后，需要配置仓库SSH私钥，具体步骤参考：【[1.3 github配置ssh私钥](#_13-github配置ssh私钥)】\n\n**3 目标仓库创建Github workflow**\n\n在【[1.6 创建Github workflow](#_16-创建github-workflow)】已经讲述了如何创建Github workflow，此处不在赘述。下面采用第二种方式进行配置。将项目clone到本地，用vscode打开，根目录下创建文件`.github\\workflows\\sync-to-github.yml`{color=primary}，内容如下：\n```yml[sync-to-github.yml]\nname: Sync to Github\non:\n   schedule:\n    # 每个小时10-59分钟之间，从10分钟开始，每5分钟执行一次\n    - cron:  '10\u002F5 * * * *'\njobs:\n    sync:\n        runs-on: ubuntu-latest\n        steps:\n            - name: Sync to Github\n              uses: wearerequired\u002Fgit-mirror-action@master\n              env:\n                  # 注意在 Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY\n                  SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }}\n              with:\n                  # 注意替换为你的 Gitee源仓库地址\n                  source-repo: git@gitee.com:AnyFork\u002Fanyfork-docs-client.git\n                  # 注意替换为你的 Github 目标仓库地址\n                  destination-repo: git@github.com:AnyFork\u002Fanyfork-docs-client.git\n```\n上面配置了`on:schedule:cron`{color=primary}定时任务，时间到了定时任务设置的时间，便会触发workflow执行。\n::callout{icon=\"material-symbols:info-outline\" color=\"warning\"}\n温馨提示\u003C\u002Fbr>\n1 设定的 schedule 是UCT时间，比如设置的08:00（也就是北京时间的16:00），但实际运行的时间为北京时间17:00 至 17：20 不等。\u003C\u002Fbr>\n2 Schedule在 GitHub 操作工作流运行的高负载期间，事件可能会延迟。高负载时间包括每小时开始。为了减少延迟的可能性，请安排您的工作流在不同的时间运行。实测延迟的时间为几十分钟，或者超过一个小时，甚至在某种极端情况下，将不会执行。\u003C\u002Fbr>\n3 Schedule 设置的 cron 时刻，仅仅是工作流进行计划排队的时刻，而不是准确的运行时刻。\n::\nGitHub Actions 默认使用 UTC 时间。如果你的工作流程应该在特定本地时间执行，你可以在 YAML 文件中指定时区：\n```yml\njobs:\n  job1:\n    runs-on: ubuntu-latest\n    steps:\n    - name: Set Timezone\n      uses: szenius\u002Fset-timezone@v1.0\n      with:\n        timezoneLinux: \"Asia\u002FShanghai\"\n\n```\n对于一些不需要实时进行同步的需求，可以采用定时延迟同步方案，方便配置也简单。\n\n\n### 2.2 实时同步方式\nWebhook是一种事件驱动的通知机制，当发生特定事件（如代码推送、拉取请求创建等）时，会自动向指定的外部服务器发送HTTP POST请求（通常包含事件数据）。这种机制替代了轮询API的实时通知方式，允许开发者在事件发生时立即触发外部操作（如部署、测试或发送通知）。\n\n\nGithub和Gitee都支持Webhook功能，因此我们可以借助这一功能，在gitee代码发生提交时，触发gitee配置的自定义webhook，webhook通过调用github Api触发github actions自动执行，从而实现实时同步方式。\n\n\n\n\n## 3 Gitee自动同步Github\n## 4 Gitee从Github同步更新\n",{"title":5,"description":833},{"loc":843,"images":847},[848,849,850,851,852,853],{"loc":235},{"loc":245},{"loc":255},{"loc":268},{"loc":271},{"loc":274},"blog\u002F2.github仓库代码和gitee仓库代码双向自动同步方案",[302,312],[857,859],{"name":97,"icon":858,"symbol":97,"id":302},"simple-icons:gitee",{"name":860,"symbol":860,"icon":861,"id":312},"github","ri:github-fill","2026-03-12 10:02:23",2880,"HXvbCFijzlprXNJ_6Ke-eAlsWps6lq7wkNS4-bskXuk",[866,871],{"title":867,"path":868,"stem":869,"articleId":870,"children":-1},"Algolia DocSearch快速开启网站全文检索功能","\u002Fblog\u002Falgolia-docsearch","blog\u002F1.Algolia DocSearch快速开启网站全文检索功能","algolia-docSearch",{"title":872,"path":873,"stem":874,"articleId":875,"children":-1},"网页字体设置：默认字体、font-family 和 @font-face 使用指南","\u002Fblog\u002Ffont-family-@font-face","blog\u002F3.网页字体设置：默认字体、font-family 和 @font-face 使用指南","fonts",1787994232405]