TOC

Git 仓库迁移学习

看到一篇公众号文章 《Git仓库迁移实操(附批量迁移脚本)》,介绍他们将 GitLab 中一个 Group 内的几十个项目迁移到另一个 Group。
PS:文章有提到,前提是无法得到管理员协助,开启创建时导入的功能。

  1. git clone & git push && git push --tags
  2. git clone --mirror && git push --mirror
  3. git clone --bare && git push --mirror

基本方法就是 clone && push,不过参数不同。
只是,我没有了解过这里说的 --mirror 参数,这里记录一下,用到的时候研究研究。

文章带了两个脚本:

  • Linux migrate.sh

    #!/bin/bash
    
    remote_old=git@host1:group1
    remote_new=git@host2:group2
    
    while read repo
    do
        echo $repo
        git clone --bare "$remote_old/${repo}.git"
        cd "${repo}.git"
        git push --mirror "$remote_new/${repo}.git"
        cd ..
        rm -fr "${repo}.git"
    done < repos.txt
    
  • Windows migrate.bat

    @echo off
    
    set remote_old=git@host1:group1
    set remote_new=git@host2:group2
    set input_file=repos.txt
    
    SETLOCAL DisableDelayedExpansion
    FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ %input_file%"`) do (
        call :process %%a
    )
    goto :eof
    
    :process
    SETLOCAL EnableDelayedExpansion
    set "repo=!%1!"
    set "repo=!repo:*:=!"
    echo !repo!
    git clone --bare "%remote_old%/!repo!.git"
    cd "!repo!.git"
    git push --mirror "%remote_new%/!repo!.git"
    cd ..
    rmdir "!repo!.git"
    ENDLOCAL
    goto :eof