You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
13 KiB
13 KiB
nnet 多仓库发布指南
一、Git多远程仓库配置
1.1 添加多个远程仓库
# 查看当前远程仓库
git remote -v
# 添加GitHub远程仓库(通常命名为origin)
git remote add origin https://github.com/noahlann/nnet.git
# 添加Gitee远程仓库
git remote add gitee https://gitee.com/noahlann/nnet.git
# 添加私有库远程仓库
git remote add private https://your-private-git.com/noahlann/nnet.git
# 或者使用SSH方式
git remote add origin git@github.com:noahlann/nnet.git
git remote add gitee git@gitee.com:noahlann/nnet.git
git remote add private git@your-private-git.com:noahlann/nnet.git
1.2 查看所有远程仓库
git remote -v
输出示例:
origin https://github.com/noahlann/nnet.git (fetch)
origin https://github.com/noahlann/nnet.git (push)
gitee https://gitee.com/noahlann/nnet.git (fetch)
gitee https://gitee.com/noahlann/nnet.git (push)
private https://your-private-git.com/noahlann/nnet.git (fetch)
private https://your-private-git.com/noahlann/nnet.git (push)
1.3 修改远程仓库URL
如果需要修改已存在的远程仓库URL:
# 修改origin的URL
git remote set-url origin https://github.com/noahlann/nnet.git
# 修改gitee的URL
git remote set-url gitee https://gitee.com/noahlann/nnet.git
二、同时推送到多个仓库
2.1 方法1:使用多个push命令(推荐)
# 推送到GitHub
git push origin main
# 推送到Gitee
git push gitee main
# 推送到私有库
git push private main
2.2 方法2:配置push URL(一次push到多个仓库)
# 为origin添加多个push URL
git remote set-url --add --push origin https://github.com/noahlann/nnet.git
git remote set-url --add --push origin https://gitee.com/noahlann/nnet.git
git remote set-url --add --push origin https://your-private-git.com/noahlann/nnet.git
# 现在只需要一个push命令
git push origin main
注意:这种方法会同时推送到所有配置的URL,但fetch时只会从第一个URL获取。
2.3 方法3:使用Git别名(推荐用于日常开发)
创建便捷的别名:
# 创建推送所有仓库的别名
git config alias.pushall '!git push origin main && git push gitee main && git push private main'
# 使用别名
git pushall
或者添加到 .git/config 文件:
[alias]
pushall = !git push origin main && git push gitee main && git push private main
2.4 方法4:使用脚本(最灵活)
创建 scripts/push-all.sh:
#!/bin/bash
# 推送所有远程仓库
echo "Pushing to GitHub..."
git push origin main || echo "Failed to push to GitHub"
echo "Pushing to Gitee..."
git push gitee main || echo "Failed to push to Gitee"
echo "Pushing to Private..."
git push private main || echo "Failed to push to Private"
echo "Done!"
使用:
chmod +x scripts/push-all.sh
./scripts/push-all.sh
三、Go模块路径配置
3.1 问题:Go模块路径的选择
Go的 go.mod 中只能有一个 module 路径,但不同仓库可能有不同的URL。
3.2 解决方案
方案1:使用GitHub作为主路径(推荐,当前采用)
// go.mod
module github.com/noahlann/nnet
go 1.25
优点:
- GitHub是Go生态中最常用的
- 大多数Go工具默认支持GitHub
- 社区熟悉
- 无需额外配置
缺点:
- 如果GitHub不可访问,会有问题
当前配置:
- 模块路径:
github.com/noahlann/nnet - 无需域名配置
方案2:使用Gitee作为主路径(国内用户)
// go.mod
module gitee.com/yourusername/nnet
go 1.25
优点:
- 国内访问速度快
- 适合国内开发者
缺点:
- 国际开发者可能不熟悉
方案3:使用自定义域名
// go.mod
module go.noahlan.cn/nnet
go 1.25
优点:
- 不依赖特定平台
- 可以随时切换仓库
- 更专业
- 更好的控制权
缺点:
- 需要配置域名和重定向
3.3 配置Go代理和私有仓库
配置GOPROXY(支持多个源)
# 设置Go代理(按优先级)
go env -w GOPROXY=https://goproxy.cn,direct
# 或者使用多个代理
go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
配置GOPRIVATE(私有仓库)
# 设置私有仓库(不走代理)
go env -w GOPRIVATE=your-private-git.com/*,gitee.com/yourusername/*
配置GOSUMDB(校验和数据库)
# 对于私有仓库,禁用校验和检查
go env -w GOSUMDB=off
# 或者只对特定域名禁用
go env -w GOSUMDB="sum.golang.org+your-private-git.com"
四、完整配置示例
4.1 项目结构
nnet/
├── .git/
├── .gitignore
├── go.mod
├── go.sum
├── scripts/
│ └── push-all.sh
└── README.md
4.2 Git配置(.git/config)
[remote "origin"]
url = https://github.com/noahlann/nnet.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "gitee"]
url = https://gitee.com/noahlann/nnet.git
fetch = +refs/heads/*:refs/remotes/gitee/*
[remote "private"]
url = https://your-private-git.com/noahlann/nnet.git
fetch = +refs/heads/*:refs/remotes/private/*
[alias]
pushall = !git push origin main && git push gitee main && git push private main
4.3 go.mod配置
module github.com/noahlann/nnet
go 1.25
require (
github.com/panjf2000/gnet/v2 v2.9.5
)
注意:使用 GitHub 作为模块路径,无需额外配置。
4.4 推送脚本(scripts/push-all.sh)
#!/bin/bash
set -e
echo "🚀 Pushing to all repositories..."
# 检查是否有未提交的更改
if ! git diff-index --quiet HEAD --; then
echo "⚠️ Warning: You have uncommitted changes"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# 获取当前分支
BRANCH=$(git branch --show-current)
echo "📦 Current branch: $BRANCH"
echo ""
# 推送到GitHub
echo "📤 Pushing to GitHub (origin)..."
if git push origin "$BRANCH"; then
echo "✅ GitHub: Success"
else
echo "❌ GitHub: Failed"
exit 1
fi
# 推送到Gitee
echo "📤 Pushing to Gitee..."
if git push gitee "$BRANCH"; then
echo "✅ Gitee: Success"
else
echo "❌ Gitee: Failed"
exit 1
fi
# 推送到私有库
echo "📤 Pushing to Private..."
if git push private "$BRANCH"; then
echo "✅ Private: Success"
else
echo "❌ Private: Failed"
exit 1
fi
echo ""
echo "🎉 All repositories updated successfully!"
4.5 Makefile配置(可选)
.PHONY: push-all
push-all:
@echo "Pushing to all repositories..."
@git push origin main
@git push gitee main
@git push private main
@echo "Done!"
.PHONY: setup-remotes
setup-remotes:
@echo "Setting up remote repositories..."
@git remote add origin https://github.com/noahlann/nnet.git || true
@git remote add gitee https://gitee.com/noahlann/nnet.git || true
@git remote add private https://your-private-git.com/noahlann/nnet.git || true
@echo "Remotes configured!"
.PHONY: sync-all
sync-all: push-all
@echo "Synced to all repositories"
使用:
make push-all
五、CI/CD配置
5.1 GitHub Actions(自动同步到其他仓库)
创建 .github/workflows/sync-repos.yml:
name: Sync to Multiple Repos
on:
push:
branches: [ main, master ]
workflow_dispatch:
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Push to Gitee
env:
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
run: |
git remote add gitee https://oauth2:${GITEE_TOKEN}@gitee.com/noahlann/nnet.git || true
git push gitee main --force || true
- name: Push to Private
env:
PRIVATE_TOKEN: ${{ secrets.PRIVATE_TOKEN }}
PRIVATE_URL: ${{ secrets.PRIVATE_URL }}
run: |
git remote add private https://oauth2:${PRIVATE_TOKEN}@${PRIVATE_URL}/noahlann/nnet.git || true
git push private main --force || true
5.2 Gitee Go(Gitee的CI/CD)
创建 .gitee/workflows/sync.yml:
name: Sync to Other Repos
on:
push:
branches: [ main ]
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Push to GitHub
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git remote add github https://x-access-token:${GITHUB_TOKEN}@github.com/noahlann/nnet.git || true
git push github main --force || true
六、最佳实践
6.1 仓库同步策略
-
主仓库选择:
- 选择一个作为"主仓库"(通常是GitHub)
- 其他仓库作为镜像
-
分支管理:
- 保持所有仓库的分支结构一致
- 使用相同的分支名称(main/master)
-
标签同步:
# 推送标签到所有仓库 git push origin --tags git push gitee --tags git push private --tags
6.2 权限管理
-
使用Personal Access Token:
- GitHub: Settings → Developer settings → Personal access tokens
- Gitee: 设置 → 私人令牌
- 私有库: 根据平台配置
-
使用SSH密钥(推荐):
# 生成SSH密钥 ssh-keygen -t ed25519 -C "your_email@example.com" # 添加到各个平台 # GitHub: Settings → SSH and GPG keys # Gitee: 设置 → SSH公钥
6.3 版本发布流程
# 1. 创建标签
git tag -a v1.0.0 -m "Release v1.0.0"
# 2. 推送代码和标签到所有仓库
git push origin main --tags
git push gitee main --tags
git push private main --tags
# 3. 在各个平台创建Release
# GitHub: Releases → New release
# Gitee: 发行版 → 新建发行版
6.4 处理冲突
如果不同仓库有不同的提交:
# 1. 从主仓库拉取
git pull origin main
# 2. 合并其他仓库的更改
git pull gitee main --allow-unrelated-histories
git pull private main --allow-unrelated-histories
# 3. 解决冲突后推送到所有仓库
git pushall
七、常见问题
7.1 如何只推送到特定仓库?
# 只推送到GitHub
git push origin main
# 只推送到Gitee
git push gitee main
7.2 如何删除远程仓库?
# 删除gitee远程仓库
git remote remove gitee
7.3 如何查看远程仓库的差异?
# 查看origin和gitee的差异
git fetch origin
git fetch gitee
git log origin/main..gitee/main
7.4 如何处理不同仓库的README?
如果需要在不同仓库显示不同的README:
# 使用分支或文件
# 方案1:使用不同的分支
git checkout -b gitee-readme
# 修改README
git push gitee gitee-readme:main
# 方案2:使用不同的文件名
# README.md (GitHub)
# README.gitee.md (Gitee)
八、推荐配置
8.1 日常开发流程
# 1. 开发完成后提交
git add .
git commit -m "feat: add new feature"
# 2. 推送到所有仓库
git pushall
# 或
./scripts/push-all.sh
8.2 版本发布流程
# 1. 更新版本号
vim version.go
# 2. 提交更改
git add .
git commit -m "chore: bump version to v1.0.0"
# 3. 创建标签
git tag -a v1.0.0 -m "Release v1.0.0"
# 4. 推送所有内容
git pushall
git push origin --tags
git push gitee --tags
git push private --tags
九、使用提供的脚本
9.1 快速配置(推荐)
# 1. 运行配置脚本
chmod +x scripts/setup-remotes.sh
./scripts/setup-remotes.sh
# 脚本会引导您配置所有远程仓库
9.2 使用推送脚本
# 1. 给脚本添加执行权限
chmod +x scripts/push-all.sh
# 2. 推送到所有仓库(使用当前分支)
./scripts/push-all.sh
# 3. 推送到指定分支
./scripts/push-all.sh main
./scripts/push-all.sh develop
9.3 脚本功能
push-all.sh 功能:
- ✅ 自动检测当前分支
- ✅ 检查未提交的更改
- ✅ 验证远程仓库配置
- ✅ 推送到所有配置的仓库
- ✅ 自动推送标签
- ✅ 彩色输出和错误处理
setup-remotes.sh 功能:
- ✅ 交互式配置远程仓库
- ✅ 支持更新已存在的远程仓库
- ✅ 自动创建Git别名
- ✅ 验证配置结果
十、总结
10.1 推荐方案
- 使用提供的脚本:
./scripts/setup-remotes.sh和./scripts/push-all.sh - 使用GitHub作为主路径:go.mod使用
github.com/noahlann/nnet(当前配置) - 配置CI/CD自动同步:减少手动操作
- 使用SSH密钥:更安全便捷
10.2 快速开始
# 方式1:使用脚本(推荐)
chmod +x scripts/setup-remotes.sh
./scripts/setup-remotes.sh
# 方式2:手动配置
git remote add origin https://github.com/noahlann/nnet.git
git remote add gitee https://gitee.com/noahlann/nnet.git
git remote add private https://your-private-git.com/noahlann/nnet.git
# 推送
./scripts/push-all.sh
# 或
git pushall # 如果已创建别名
10.3 工作流程
# 日常开发
git add .
git commit -m "feat: new feature"
./scripts/push-all.sh
# 版本发布
git tag -a v1.0.0 -m "Release v1.0.0"
./scripts/push-all.sh
git push origin --tags
git push gitee --tags
git push private --tags
文档版本: v1.0
最后更新: 2024