方式一 简单代码迁移
简单代码迁移,顾名思义,就是只迁移代码到新的地址,代码在新的地址开不了之前的提交记录。
1、登录原Gitlab页面,选择要迁移的项目,复制地址
2、使用git小乌龟克隆项目到本地
3、拉却需要的分支出来
4、登录新的Gitlab页面,创建同名的项目
创建项目方式参考:
5、克隆新地址的项目到本地并拉取分支
6、把旧项目名目录下的的代码复制到新项目名目录下
7、进入到新项目名目录下,提交代码并推送。
方式二 Gitlab(整个项目)全库迁移
用命令行实现Gitlab上某个项目的全库迁移,包括该项目的各个分支、提交记录以及标签。
1、首先登录到新地址的Gitlab页面,新建一个项目(创建项目方式参考:),这时新建的项目是没有分支的,如图
2、点击Git Bash Here 使用命令把旧地址的项目克隆到本地
命令如下:
1 git clone http://*.*.*.*/path/项目名称.git #旧项目仓库地址
2 cd 项目名称/
3 git branch
4 git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote";done
5 git fetch --all
6 git fetch --tags
执行结果如下:
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ ls #列出当前路径下文件
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ pwd #查看当前路径
/d/.zidingyi/move-projects
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ git clone http://10.10.2.66/zeus/sandbox.git #把远程仓库的项目克隆到本地
Cloning into 'sandbox'...
remote: Counting objects: 252, done.
remote: Compressing objects: 100% (152/152), done.
Receiving objects: 81% (205/252), 2.44 MiB |remote: Total 252 (delta 34), reused 204 (delta 25)
Receiving objects: 100% (252/252), 2.61 MiB | 197.00 KiB/s, done.
Resolving deltas: 100% (34/34), done.
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ ls #再次查看当前路径下文件,可以看到已经把sandbox这个项目克隆到本地
sandbox/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ cd sandbox/ #进入到项目名目录下
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ ls #查看该项目路径下的文件
pom.xml README.md sandbox-batch/ sandbox-service-api/ src/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git branch -r #查看远程分支
origin/HEAD -> origin/master
origin/dev
origin/dev_1.0.0
origin/dev_1.1.0
origin/dev_1.4.0
origin/develop
origin/master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git branch #查看本地分支
* master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote";done #切换出全部分支
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Branch 'dev_1.0.0' set up to track remote branch 'dev_1.0.0' from 'origin'.
Branch 'dev_1.1.0' set up to track remote branch 'dev_1.1.0' from 'origin'.
Branch 'dev_1.4.0' set up to track remote branch 'dev_1.4.0' from 'origin'.
Branch 'develop' set up to track remote branch 'develop' from 'origin'.
fatal: A branch named 'master' already exists.
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git branch #再次查看本地分支,可以看到本地已切出全部分支
dev
dev_1.0.0
dev_1.1.0
dev_1.4.0
develop
* master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git fetch --all #获取该项目远程库的所有分支及其内容
Fetching origin
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git fetch --tags #获取该项目远程库的标签
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ ls
pom.xml README.md sandbox-batch/ sandbox-service-api/ src/
3、把本地整个项目打包压缩,移到需要的路径下面,(也可以在当前路径直接提交代码到新地址)
4、点击Git Bash Here 使用命令把代码提交到远程Gitlab库
命令如下:
1 git remote rename origin old-origin
2 git remote add origin http://*.*.*.*/path/项目名称.git #项目的新仓库地址
3 git push origin --all
4 git push --tags
执行结果:
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test
$ ls
sandbox/
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test
$ cd sandbox/
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test/sandbox (master)
$ ls
pom.xml README.md sandbox/ sandbox-batch/ sandbox-service-api/ src/
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test/sandbox (master)
$ git branch
dev
dev_1.0.0
dev_1.1.0
dev_1.4.0
develop
* master
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test/sandbox (master)
$ git remote rename origin old-origin #将原来的origin重命名一下
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test/sandbox (master)
$ git remote add origin http://110.110.2.10:8010/zeus/sandbox.git #指定需要迁移到的目标地址
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test/sandbox (master)
$ git push origin --all #推送所有分支及其内容
Enumerating objects: 251, done.
Counting objects: 100% (251/251), done.
Delta compression using up to 4 threads
Compressing objects: 100% (142/142), done.
Writing objects: 100% (251/251), 2.61 MiB | 19.25 MiB/s, done.
Total 251 (delta 34), reused 251 (delta 34), pack-reused 0
remote: Resolving deltas: 100% (34/34), done.
To http://110.110.2.10:8010/zeus/sandbox.git
* [new branch] dev -> dev * [new branch] dev_1.0.0 -> dev_1.0.0
* [new branch] dev_1.1.0 -> dev_1.1.0
* [new branch] dev_1.4.0 -> dev_1.4.0
* [new branch] develop -> develop
* [new branch] master -> master
user@▒ҿ▒▒▒▒▒▒▒▒▒▒▒▒ MINGW64 /d/old-origin/move-test/sandbox (master)
$ git push --tags #推送标签
5、刷新(新地址)Gitlab页面,就可以看到之前的所有分支等信息
方式三 迁移需要的分支
用命令行实现Gitlab上某个项目的的某个(某几个)分支的全部内容的迁移,具体步骤与方式二相似;参考一路追寻的博客:
1、命令如下:
1 git clone http://*.*.*.*/path/项目名称.git #项目的旧仓库地址
2 cd 项目名称/
3 git branch
4 git checkout 分支名
5 git fetch --all
6 git fetch --tags
7 git remote rename origin old-origin
8 git remote add origin http://*.*.*.*/path/项目名称.git #项目的新仓库地址
9 git checkout 分支名
10 git push origin --all
11 git push --tags
2、执行结果如下:
克隆旧地址的项目到本地,切出需要的分支:
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ pwd
/d/.zidingyi/move-projects
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ ls
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$
Cloning into 'sandbox'...
remote: Counting objects: 252, done.
remote: Compressing objects: 100% (152/152), done.
remote: Total 252 (delta 34), reused 204 (delta 25)
Receiving objects: 100% (252/252), 2.61 MiB | 420.00 KiB/s, done.
Resolving deltas: 100% (34/34), done.
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ ls
sandbox/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects
$ cd sandbox/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ ls
pom.xml README.md sandbox-batch/ sandbox-service-api/ src/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git branch
* master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git branch -r
origin/HEAD -> origin/master
origin/dev
origin/dev_1.0.0
origin/dev_1.1.0
origin/dev_1.4.0
origin/develop
origin/master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (master)
$ git checkout dev_1.4.0
Switched to a new branch 'dev_1.4.0'
Branch 'dev_1.4.0' set up to track remote branch 'dev_1.4.0' from 'origin'.
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev_1.4.0)
$ ls
pom.xml README.md src/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev_1.4.0)
$ git fetch --all
Fetching origin
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev_1.4.0)
$ git fetch --tags
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev_1.4.0)
$ ls
pom.xml README.md src/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev_1.4.0)
$ git branch
* dev_1.4.0
master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev_1.4.0)
$ git checkout dev
Switched to a new branch 'dev'
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git fetch --all
Fetching origin
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git fetch --tags
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ ls
pom.xml README.md sandbox-batch/ sandbox-service-api/ src/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git branch
* dev
dev_1.4.0
master
提交代码到新地址的Gitlab仓库:
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ ls
pom.xml README.md sandbox-batch/ sandbox-service-api/ src/
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git remote rename origin old-origin
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git remote add origin http://10.110.20.10:8086/zeus/sandbox.git #关联新地址的Gitlab仓库
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git remote -v #检查是否关联成功
old-origin http://10.120.10.120/zeus/sandbox.git (fetch) #旧地址
old-origin http://10.120.10.120/zeus/sandbox.git (push)
origin http://10.110.20.10:8086/zeus/sandbox.git (fetch) #新地址
origin http://10.110.20.10:8086/zeus/sandbox.git (push)
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git push origin --all
Enumerating objects: 245, done.
Counting objects: 100% (245/245), done.
Delta compression using up to 4 threads
Compressing objects: 100% (139/139), done.
Writing objects: 100% (245/245), 2.61 MiB | 17.48 MiB/s, done.
Total 245 (delta 32), reused 243 (delta 31), pack-reused 0
remote: Resolving deltas: 100% (32/32), done.
remote:
remote: To create a merge request for dev, visit:
remote: http://10.110.20.10:8086/zeus/sandbox/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
remote: To create a merge request for dev_1.4.0, visit:
remote: http://10.110.20.10:8086/zeus/sandbox/merge_requests/new?merge_request%5Bsource_branch%5D=dev_1.4.0
remote:
To http://10.110.20.10:8086/zeus/sandbox.git
* [new branch] dev -> dev
* [new branch] dev_1.4.0 -> dev_1.4.0
* [new branch] master -> master
usrname@DESKTOP-C425NA9 MINGW64 /d/.zidingyi/move-projects/sandbox (dev)
$ git push origin --tags
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 147 bytes | 147.00 KiB/s, done.
Total 1 (delta 0), reused 1 (delta 0), pack-reused 0
To http://10.110.20.10:8086/zeus/sandbox.git
* [new tag] 1.1.0 -> 1.1.0
3、登录Gitlab页面检查所需要的分支是否已经提交
逆风的方向,更适合飞翔,我不怕千万人阻挡,只怕自己投降。