前言
作为开发者,你是否遇到过这样的困扰:想同时享受GitHub的全球开源生态和Gitee的国内高速访问,但手动同步代码太麻烦?本文将手把手教你三种高效同步方案,从此告别重复操作!
GitHub优势:全球最大的开源社区,项目曝光率高
Gitee优势:国内访问速度快,适合团队协作和文档托管
双保险:避免单一平台故障导致代码丢失
适用场景:需要分别管理不同平台
# 添加GitHub远程仓库
git remote add github https://github.com/你的用户名/仓库名.git
# 添加Gitee远程仓库
git remote add gitee https://gitee.com/你的用户名/仓库名.git
# 查看配置结果
git remote -v
推送操作:
# 推送到GitHub
git push github main
# 推送到Gitee
git push gitee main
适用场景:想一次推送完成多平台同步
# 初始化远程仓库
git remote add origin https://github.com/你的用户名/仓库名.git
# 添加Gitee地址
git remote set-url --add origin https://gitee.com/你的用户名/仓库名.git
# 验证配置
git remote -v
origin https://github.com/your-username/your-repo-name.git (fetch)
origin https://github.com/your-username/your-repo-name.git (push)
origin https://gitee.com/your-username/your-repo-name.git (push) # 如果设置了同步推送
gitee https://gitee.com/your-username/your-repo-name.git (fetch)
gitee https://gitee.com/your-username/your-repo-name.git (push)
一键推送:
git push origin main
# 添加修改文件
git add .
# 提交更改
git commit -m "修复登录页样式问题"
# 根据方案选择推送方式
git push github main # 方案一
git push origin main # 方案二
# 从指定平台拉取
git pull github main
git pull gitee main
# 合并多个源更新
git pull --all
首先确认两个远程仓库的状态:
# 获取GitHub远程仓库最新状态
git fetch origin
# 获取Gitee远程仓库最新状态
git fetch gitee
比较两个仓库的分支差异:
# 比较main分支
git diff origin/main gitee/main
假设GitHub上的代码是最新的,需要更新Gitee:
# 确保本地分支与GitHub同步
git checkout main
git pull origin main
# 强制推送到Gitee
git push -f gitee main
或者相反,如果Gitee上的代码是最新的:
git checkout main
git pull gitee main
git push -f origin main
如果两个仓库都有独立的修改,需要合并:
# 创建临时分支
git checkout -b temp_sync_branch
# 合并GitHub的修改
git pull origin main
# 合并Gitee的修改(可能需要解决冲突)
git pull gitee main
# 解决冲突后提交
git add .
git commit -m "合并GitHub和Gitee的修改"
# 推送到两个远程仓库
git push origin temp_sync_branch:main
git push gitee temp_sync_branch:main
# 切回main分支并更新
git checkout main
git pull origin main
对于差异较大的情况,可以使用镜像克隆完全刷新一个仓库:
# 创建临时目录
mkdir temp_repo && cd temp_repo
# 镜像克隆最新的仓库(假设GitHub是最新的)
git clone --mirror https://github.com/username/repo-name.git
# 进入镜像仓库
cd repo-name.git
# 添加Gitee为远程仓库
git remote add gitee https://gitee.com/username/repo-name.git
# 推送镜像到Gitee
git push --mirror gitee
为避免今后出现不一致问题:
git remote set-url --add origin https://gitee.com/username/repo-name.git
#!/bin/bash
# 同步脚本
git fetch --all
git checkout main
git pull origin main
git push gitee main
git push origin main