用克隆方式转移 Git 远程仓库

TL;DR: 直接拉到最后看第三步

一、步骤:克隆 —> 设置新远程 —> Push

涉及命令:git clone、git remote [ set-url | rename | add | -v ]、git push [ --all | -u | --tags ]

## 克隆
$ git clone [remote repository address]        #克隆原仓库
## 转移方法 1 (两行命令法)
$ git remote set-url origin gitcode.xxx-new.com/xxx.git   #设置新远程
$ git push origin --all                        #推送所有分支
## 转移方法 2 (保留。两个远程仓库法):
$ git remote -v								   #查看目前的连接了哪些远程连接
$ git remote add new_origin ssh://[email protected]/xxx/xxpay.git       #新远程
$ git push -u new_origin --all   #-u代表upstream,以后默认使用新连接推送
$ git push origin --tags  		#转移标签, 没有可省略
## End
## 补充
$ git remote rename origin old-origin      #改名

二、如何切换本地分支,使转移后的分支完整

涉及命令:git checkout [ -t | -b ]、git push、git clone [ -b | - a | -r ]、git fetch [ --all ]

直接clone下来的代码看着只有一个分支,但是其实本地已经包含了所有分支,只是需要你去切换。(因为存在转移后,漏掉其他分支的情况,你需要再检查一下,如果漏掉,请使用以下方法拉取所有分支后,再push一遍)

## 转移仓库分支
$ git checkout -t origin/branchName  #本地新建远程对应分支
## equal: git checkout -b branchName origin/branchName
## 新建远程分支 (推送我们新建的分支到远程)
$ git push origin [localBranchName]:[newRemoteBranchName] 
## End
## 补充
# 克隆同时选定分支
$ git clone -b <branch name> [remote repository address]
# 查看所有分支, 包括远程分支
$ git branch -a
# 查看所有远程分支
$ git branch -r
# 关联本地分支与远程分支 (不加-b)
$ git checkout branchName origin/branchName 
# 似乎可以拉取所有分支
$ git fetch --all

三、官方推荐的复制仓库方式

以上都是,基于我的理解进行的学习和克隆操作。下面是官方推荐复制仓库方式

## 创建仓库的裸克隆
$ git clone --bare https://github.com/exampleuser/old-repository.git
## 镜像推送至新仓库
$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git
## 删除您之前创建的临时本地仓库
$ cd ..
$ rm -rf old-repository.git
## End
## 如果不想删除,想将裸克隆恢复成正常仓库,请使用以下两步
$ git config --unset core.bare    # 解除core.bare模式
$ git reset --hard       # 恢复所有的repo文件

(完)

猜你喜欢

转载自blog.csdn.net/howeres/article/details/107358102