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