git推送不同分支到不同仓库库
在同一个项目中,有时需要维护国际版和国内版。可以在同一个本地 Git仓库中创建不同分支,并将不同分支分别关联到不同的远程仓库。
本文以 GitHub 和 Gitee 为例:
- master 分支推送到 GitHub。
- new_branch_gitee 分支推送到 Gitee。
- 两个平台使用不同的远程仓库地址。
- 每个本地分支分别设置自己的 upstream(上游跟踪分支)。
这样,切换到对应分支后执行 git push,Git
就会将当前分支推送到它所关联的远程仓库。
示例仓库地址均为演示地址,请替换为自己的实际仓库地址。
1. 首先我们设置我门当前仓库地址(如果有可以直接忽略看第2步骤)
# 首先看本地有没有远程地址
git remote -v
fatal: not a git repository (or any of the parent directories): .git
# 如果没有 可以
git init
# 添加远程地址
git remote add origin git@github.com:forasp/cn.git
# 如果已经添加过,可以修改
git remote set-url origin git@git.github.com:forasp/cn.git
# 这里的origin 是指定了远程仓库名称为origin ,地址为 git@git.github.com:forasp/cn.git
2.添加 Gitee 远程仓库,新建本地仓库名称以及地址指向
# 这是添加一个远程仓库
git remote add new_repo_gitee git@gitee.com:forasp/cn.git
# 这里的添加了仓库名称 new_repo_gitee 指向了gitee,查看 远程地址你会看到
git remote -v
new_repo_gitee git@gitee.com:forasp/cn.git (fetch)
new_repo_gitee git@gitee.com:forasp/cn.git (push)
origin git@github.com:forasp/cn.git (fetch)
origin git@github.com:forasp/cn.git (push)
3. 新建分支new_branch_gitee 推送到gitee仓库
# 新建分支 new_branch_gitee
git branch new_branch_gitee
# 如果远程对应分支不存在,直接推送并撞见关系
git push -u new_repo_gitee new_branch_gitee
# 如果远程分支已经存在,则远程创建关系
git branch --set-upstream-to=new_repo_gitee/new_branch_gitee new_branch_gitee
4. 你可以通过命令查看是否已经设置了分支与仓库名称以及远程地址对应
# 查看对应关系
git branch -vv
master 74a614ae [origin/master] logcontent
* new_branch_gitee c58b8390 [new_repo_gitee/new_branch_gitee] logcontent
5. 再次推送验证 即可。
切换分支,推送对应内容即可。
6. 补充git 常用命令
查看远程仓库 git remote -v
添加远程仓库 git remote add 别名 地址
修改远程仓库地址 git remote set-url 别名 地址
删除远程仓库 git remote remove 别名
查看本地分支 git branch
查看本地及远程分支 git branch -a
查看分支跟踪关系 git branch -vv
创建本地分支 git branch 分支名
创建并切换分支 git switch -c 分支名
切换分支 git switch 分支名
首次推送并设置跟踪关系 git push -u 远程别名 本地分支名
推送当前分支 git push
设置已有分支的跟踪关系 git branch --set-upstream-to=远程别名/远程分支名 本地分支名