本文介绍如何在一个本地 Git 仓库中同时使用两个远程仓库:

  • GitHub
  • CNB

目标结构:

  • GitHub:mainbg 等开发分支
  • CNB:只保留 main 分支
  • 本地开发:使用 bg 分支
  • 同步规则:
      本地 bg → GitHub bg
本地 bg → CNB main
    

一、远程仓库结构

先看当前远程仓库

      git remote -v
    

一般会看到类似:

      origin  https://github.com/xxx/your-project.git (fetch)
origin  https://github.com/xxx/your-project.git (push)
    

添加 CNB 远程仓库

假设你的 CNB 仓库地址是:

      git remote add cnb https://cnb.cool/xxx/your-project.git
    

然后确认:

      git remote -v
    

应该能看到:

      origin  https://github.com/xxx/your-project.git
origin  https://github.com/xxx/your-project.git
cnb     https://cnb.cool/xxx/your-project.git
cnb     https://cnb.cool/xxx/your-project.git
    

说明:

远程名平台
originGitHub
cnbCNB

二、本地分支与远程分支关系

查看本地分支:

      git branch -vv
    

示例:

      * bg   a1b2c3d [origin/bg] 开发提交
  main e4f5g6h [origin/main] 主分支
    

解释:

本地分支upstream
bgorigin/bg
mainorigin/main

这里可以看到:

  • bg 绑定的是 GitHub 的 bg
  • 没有绑定 CNB

因此:

VSCode push 默认只会推送到 GitHub

三、仓库关系图

                                     GitHub
              ┌─────────┐
              │ main    │
              │ bg ◄────┼───────┐
              └─────────┘       │
                                │ push
                                │
        Local Repo              │
      ┌───────────────┐         │
      │ main          │         │
      │               │         │
      │ bg ───────────┼─────────┘
      │   │
      │   │ push
      │   ▼
      │ CNB main
      └───────────────┘
    

同步关系:

      本地 bg → GitHub bg
本地 bg → CNB main
    

四、VSCode Push 行为

VSCode Push 按钮遵循 Git upstream 规则:

      本地分支 → upstream 分支
    

当前绑定:

      bg → origin/bg
    

所以 VSCode Push 会:

      本地 bg → GitHub bg
    

不会推送到 CNB。

五、同步 CNB main

如果要同步 CNB,需要手动推送:

      git push cnb bg:main
    

含义:

      本地 bg → CNB main
    

六、完整开发流程

推荐流程:

开发代码

VSCode 修改代码

提交

      git add .
git commit -m "update"
    

或 VSCode 图形界面提交

推送 GitHub

VSCode Push

      本地 bg → GitHub bg
    

同步 CNB

      git push cnb bg:main
    

七、一键同步(推荐)

配置 Git alias

      git config alias.syncbg "!git push origin bg && git push cnb bg:main"
    

使用:

      git syncbg
    

效果:

      本地 bg → GitHub bg
本地 bg → CNB main
    

push refspec

执行:

      git config remote.origin.push "refs/heads/bg:refs/heads/bg"
git config --add remote.origin.push "refs/heads/bg:refs/heads/main"
    

查看配置:

      git config --get-all remote.origin.push
    

结果:

      refs/heads/bg:refs/heads/bg
refs/heads/bg:refs/heads/main
    

含义:

      本地 bg → 远程 bg
本地 bg → 远程 main
    

现在只需要:

      git push origin
    

Git 会执行:

      bg → GitHub bg
bg → CNB main
    

VSCode Push 也会触发这个规则。

Git Sync Plugin

一个用于在不同 Git 平台之间同步代码的插件。支持通过 HTTPS 或 SSH 方式同步代码到其他 Git 托管平台。

例如从 CNB 同步到 GitHub如下

      # .cnb.yml
main:
  push:
    - stages:
        - name: sync to github
          image: tencentcom/git-sync
          settings:
            target_url: https://github.com/username/repo.git
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}
    

八、常见问题

为什么 git remote -v 有 cnb?

因为 cnb 是远程仓库配置:

      origin → GitHub
cnb → CNB
    

所有分支都可以使用这些远程。

为什么 git branch -vv 没有 cnb?

因为本地分支没有绑定 CNB upstream。

只有绑定后才会显示。

例如:

      bg [origin/bg]
    

VSCode push 能推 CNB 吗?

默认不能,因为 upstream 指向 GitHub。

可以:

  • 手动 push
  • 写脚本
  • 使用 Git alias…

九、最佳实践总结

推荐结构:

      GitHub
 ├─ main
 └─ bg

CNB
 └─ main
    

开发分支:bg

同步关系:

      bg → GitHub bg
bg → CNB main
    

优点:

  • GitHub 保留完整分支结构
  • CNB 保持单 main
  • 不影响 VSCode 图形操作
  • 同步逻辑清晰

声明

作者: liyao

版权:本博客所有文章除特别声明外,均采用CCBY-NC-SA4.O许可协议。转载请注明!

最后更新于 2026-03-07 19:35 history