三行代码实现新老git仓库完整迁移

背景:

    公司因老版本gitlib存在安全隐患且机房需要迁移,需要切换到新版本的gitlib上

难点:

如果仓库数量不多,可以通过添加远程分支方式实现代码迁移,我们有上百个git仓库需要迁移到,如果算上分支和标签的话还得*10,工作量巨大。怎么高效和快速完整迁移仓库呢?

迁移后的仓库要保证:

1、代码仓库全量迁移

2、提交历史要保留

3、分支要保留

4、标签要保留

三行代码实现

#从老gitlib拉取裸仓库,并在本地文件系统创建gitbook-demo.git文件夹
git clone --bare ssh://git@oldgitlab:12345/group1/gitbook-demo.git
#进入代码目录
cd gitbook-demo.git
#向新git推送镜像
git push --mirror ssh://git@newgitlab:67890/group1/gitbook-demo.git

关键参数说明:

  • bare
    --bare是裸仓库的意思,和我们常用的git clone区别在于,他不是一个工作空间,不可以在目录下进行变更操作,即使操作了也不被接受, 执行命令后会复制全部的分支、标签,并且在命令执行过程中的所有远端的变动均被忽略
#官网的解释 Git - git-clone Documentation (git-scm.com)
--bare
Make a bare Git repository. That is, instead of creating <directory> and placing the administrative 
files in <directory>/.git, make the <directory> itself the $GIT_DIR. This obviously implies the 
--no-checkout because there is nowhere to check out the working tree. Also the branch heads at the 
remote are copied directly to corresponding local branch heads, without mapping them to
refs/remotes/origin/. When this option is used, neither remote-tracking branches nor the related 
configuration variables are created.

看一下他的结构

.
+--- config
+--- description
+--- HEAD
+--- hooks
|   +--- applypatch-msg.sample
|   +--- commit-msg.sample
|   +--- fsmonitor-watchman.sample
|   +--- post-update.sample
|   +--- pre-applypatch.sample
|   +--- pre-commit.sample
|   +--- pre-push.sample
|   +--- pre-rebase.sample
|   +--- pre-receive.sample
|   +--- prepare-commit-msg.sample
|   +--- update.sample
+--- info
|   +--- exclude
+--- objects
|   +--- info
|   +--- pack
|   |   +--- pack-bcbf9435c77edd5760bc9fcd1897c93317b379ab.idx
|   |   +--- pack-bcbf9435c77edd5760bc9fcd1897c93317b379ab.pack
+--- packed-refs
+--- refs
|   +--- heads
|   +--- tags

普通仓库(非裸仓库)的结构

+--- COMMIT_EDITMSG
+--- config
+--- FETCH_HEAD
+--- HEAD
+--- index
+--- logs
|   +--- HEAD
|   +--- refs
|   |   +--- heads
|   |   |   +--- master
|   |   +--- remotes
|   |   |   +--- origin
|   |   |   |   +--- HEAD
|   |   |   |   +--- master
+--- objects
|   +--- 0a
|   |   +--- 5349217075b4e65f23f1fc6047bd88b3bc408a
|   +--- 0f
|   |   +--- ea4d6af00e21b71a3d35e5a830e69c09f2a899
|   |   +--- f1fa62af96492f849b465019cf887fc1bf05ec
|   +--- 11
|   |   +--- 327b90d92796e1584c91316c355c119b8f87f5
|   |   +--- bb6a4b917bb7706244d9369b9fb3cac7277752
|   +--- ff
|   |   +--- 5af373a9c41390ddbc3915978e029d9d298d29
|   +--- info
|   +--- pack
|   |   +--- pack-e872e830762b5b1425d3c782702da0646697165f.idx
|   |   +--- pack-e872e830762b5b1425d3c782702da0646697165f.pack
+--- ORIG_HEAD
+--- packed-refs
+--- refs
|   +--- heads
|   |   +--- master
|   +--- remotes
|   |   +--- origin
|   |   |   +--- HEAD
|   |   |   +--- master
|   +--- tags
+--- sourcetreeconfig

裸仓库是仓库的完整合集,非裸仓库只是含有本地仓库、跟踪仓库

[core]
    repositoryformatversion = 0
    filemode = false
    bare = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = ssh://git@oldgitlab:12345/platform/gitbook-demo.git
——————————————————————————————————————↑↑↑↑↑↑↑↑↑↑↑↑裸仓库config文件↑↑↑↑↑↑↑↑↑↑↑↑↑↑————————————————————————————————————————————
——————————————————————————————————————↓↓↓↓↓↓↓↓↓↓↓↓↓↓非裸仓库./.git/config文件↓↓↓↓↓↓↓↓↓↓↓↓————————————————————————————————————————————
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = ssh://git@oldgitlab:12345/platform/gitbook-demo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[gui]
    wmstate = normal
    geometry = 835x475+225+225 185 214
  • --mirror
    --mirror镜像代码仓库,原原本本,完完整整是他的特点。
Instead of naming each ref to push, specifies that all refs under refs/ (which includes but is not
 limited to refs/heads/, refs/remotes/, and refs/tags/) be mirrored to the remote repository. 
 Newly created local refs will be pushed to the remote end, locally updated refs will be force
  updated on the remote end, and deleted refs will be removed from the remote end. This is the
   default if the configuration option remote.<remote>.mirror is set.
#简单点说就是所有引用会以自己为主,强制推送到远程

腾云先锋(TDP,Tencent Cloud Developer Pioneer)是腾讯云 GTS 官方组建并运营的技术开发者群体。这里有最专业的开发者&客户,能与产品人员亲密接触,专有的问题&需求反馈渠道,有一群志同道合的兄弟姐妹。来加入属于我们开发者的社群吧 。

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
三行代码实现新老git仓库完整迁移
公司因老版本gitlib存在安全隐患且机房需要迁移,需要切换到新版本的gitlib上
<<上一篇
下一篇>>