如何将现有分支推送至新GitLab仓库?跨仓库分支迁移重命名方法
操作方案
一、从仓库A克隆指定分支并以新名称推送至仓库B
最优方式是用浅克隆仅拉取目标分支(避免下载仓库全量历史,提升效率),再推送至仓库B并指定新分支名:
- 克隆仓库A的目标分支(假设原分支名为
old-branch):
git clone --branch old-branch --single-branch https://gitlab.com/your-username/repo-A.git cd repo-A
- 添加仓库B作为新远程(命名为
repo-B便于区分):
git remote add repo-B https://gitlab.com/your-username/repo-B.git
- 将当前分支以新名称(比如
new-branch)推送至仓库B:
git push repo-B old-branch:new-branch
若后续无需关联仓库A,可删除原远程:
git remote remove origin
二、将本地现有分支推送至新GitLab仓库
假设本地已有分支local-branch,要推送至新仓库C:
- 添加新仓库C作为远程:
git remote add repo-C https://gitlab.com/your-username/repo-C.git
- 推送分支至新仓库(若要设置上游关联,方便后续拉取/推送,加上
-u参数):
# 仅推送分支 git push repo-C local-branch # 推送并设置上游关联 git push -u repo-C local-branch
如果需要给远程分支改名,格式如下:
git push repo-C local-branch:new-remote-branch
内容的提问来源于stack exchange,提问作者mak_doni
相关产品推荐
相关产品推荐

