git - 将存储库从 Github 迁移到自托管 Gitlab 时更新和修复 LFS

标签 git bash github gitlab

对于我的公司,我必须将公司所有存储库从 Github 迁移到我们自托管的 Gitlab-Server。

我遇到的主要问题是我们使用 Git-LFS对于我们的大多数存储库。

因此,在克隆存储库时,我必须通过运行来修复 Git-LFS

git lfs fetch --all
git lfs push remoteOnGitlab

我正在为此编写一个脚本 - 因为它有很多存储库 - 使用 Github API 来处理我们所有存储库的列表:

#!/bin/bash

###_Adjust those_#######################################################
githubUrlStart="https://"
githubLink="github.com/<orgaName>"
githubApiLink="api.github.com/orgs/<orgaName>/repos"

# maxRepoCount is needed if the organisation has more than 30 repos
# otherwise only the first 30 repos would be mirrored
maxRepoCount="200"

gitlabUrlStart="http://"
gitlabLink="<gitlabURL>/<groupName>"
########################################################################


###_Get Sensible Data at runtime_###
bold=$(tput bold)
normal=$(tput sgr0)
                                           
echo -n "${bold}Github Username: ${normal}"
read -e githubName
echo -n "${bold}Github Password: ${normal}"
read -e -s githubPassword
echo ""
echo -n "${bold}Gitlab Username: ${normal}"
read -e gitlabName
echo -n "${bold}Gitlab Password: ${normal}"
read -e -s gitlabPassword

###_Add folder for cloned git repos_###
mkdir gits

###_GET JSON of all REPOS from the Github API_###
# writes output to file test for later use
curl "$githubUrlStart${githubName//\"}:${githubPassword//\"}@${githubApiLink//\"}?per_page=${maxRepoCount//\"}" > test 

###_Get URLS and NAME_###
#  -> http://xmodulo.com/how-to-parse-json-string-via-command-line-on-linux.html #
NAMES=$(cat test | jq ".[] | .name");

###_Transform into Array_###
NAMES_ARRAY=()

COUNT=0
for name in $NAMES
do
    NAMES_ARRAY[$COUNT]=$name
                             
    COUNT=$((COUNT+1))
done


###_Clone each repo local and use it to repair git lfs_###                                                                                            
COUNT=0                                                                                       
for reponame in "${NAMES_ARRAY[@]}"                                                           
do                                                                                            
    ###_Build full link for cloning_###
    fulllink="$githubUrlStart$githubName:$githubPassword@$githubLink/${reponame//\"}"     
                                                                                              
    ###_Clone from github using git lfs_###
    git lfs clone "${fulllink//\"}" "gits/${reponame//\"}"
                                                                                              
    ###_Enter folder of cloned repository_###                                                   
    cd "gits/${reponame//\"}"
                                                                                              
    ###_repair git lfs_###
    # get lfs from github
    git lfs fetch --all
    
    # add the remote for gitlab
    gitlabRemote="$gitlabUrlStart$gitlabName:$gitlabPassword@$gitlabLink/${reponame//\"}.git" 
    git remote add gitlab "${gitlabRemote//\"}"
    
    # Just for security remove the github remote                                                  
    git remote remove origin                                                                  
    
    # Push lfs to the gitlab remote
    git lfs push gitlab --all                                                                 
       
    ##_leave folder!_##                                                                       
    cd ../..          #                                                                       
   
    ##_remove folder to save disk space_##                                                    
    rm -rf gits/${reponame//\"}
                                                                                              
    COUNT=$((COUNT+1))
done

###_Remove the gits folder_####
rm -rf gits

我的问题是:
看起来这只能正常工作一次,但是如果我再次运行脚本,存储库不会更新到最新状态。我也不确定,但我想到目前为止它只能在 master 分支上工作。

为了始终在 gitlab 上从 github 获取 git repo 的最新状态,我需要更改什么?(运行后,我仍然拥有 2 个月前在 Gitlab 上的状态) ...)


替代方案

我还看到了另一个解决方案here这显然可以解决多分支问题:

git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch

我可能也可以与之结合

git push --mirrow path/to/otherRemote

正如评论中提到的。

这当然会更简单..但是:
这与 LFS 兼容吗? 比如说作为

git lfs clone --mirrow /originOnGithub /localFolder
cd /localFolder
git lfs fetch --all
git lfs push --mirrow /remoteOnGitlab

最佳答案

答案是:是的!

但是我有一个非常愚蠢的拼写错误:当然是--mirror而不是--mirrow

现在为每个项目运行一次

git lfs clone --mirror /originOnGithub /localFolder
cd /localFolder
git lfs fetch --all
git lfs push --mirror /remoteOnGitlab

它会修复 LFS 文件并且更新 Gitlab 服务器上存储库中的所有分支。

关于git - 将存储库从 Github 迁移到自托管 Gitlab 时更新和修复 LFS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47528550/

相关文章:

Bash:读取输入可用(是否按下任何键?)

bash - bash 中的整数比较

azure - SvelteKit 应用程序未在 Azure Web Static App 中上传

github - 如何在 github 中创建 .md 文件?

Git:如何通过.gitignore 忽略 stash 文件/点文件/文件名为空的文件?

git - 清理提交日志质量较差的 git 分支

git - 如何获取本地 PC 上的所有远程分支?

linux - curl 命令输出编码错误

更新 Homebrew 软件 OS X 时的 Git 警告

git - 在 Gitlab 中获取贡献