如何使用CLI(Shell脚本)将现有git repo从本地服务器传输到gitlab repo?

如何使用CLI(Shell脚本)将现有git repo从本地服务器传输到gitlab repo?,git,shell,github,gitlab,command-line-interface,Git,Shell,Github,Gitlab,Command Line Interface,我的服务器上有很多git回购协议。我想使用命令行(脚本)在上创建新的gitlab repo,形成现有的本地repo。并在gitLab服务器上推送所有带有标记的分支 没有企业帐户是否可以这样做?您可以将gitlab repo作为远程服务器添加到服务器中。然后你可以使用push-all 在您拥有的每个回购目录中都有类似的内容: git add remote gitlab <YOUR NEW REPO URL> git push gitlab --all git添加远程gitlab gi

我的服务器上有很多git回购协议。我想使用命令行(脚本)在上创建新的gitlab repo,形成现有的本地repo。并在gitLab服务器上推送所有带有标记的分支


没有企业帐户是否可以这样做?

您可以将gitlab repo作为远程服务器添加到服务器中。然后你可以使用push-all

在您拥有的每个回购目录中都有类似的内容:

git add remote gitlab <YOUR NEW REPO URL>
git push gitlab --all
git添加远程gitlab
git推gitlab——全部

您可以将gitlab repo作为远程服务器添加到服务器中。然后你可以使用push-all

在您拥有的每个回购目录中都有类似的内容:

git add remote gitlab <YOUR NEW REPO URL>
git push gitlab --all
git添加远程gitlab
git推gitlab——全部
我的frined为此构建了一个脚本(需要curl、git、jq)。我们使用它,它工作得很好:

要查找名称空间,最好快速查看API:

curl "https://example.com/api/v3/projects?private_token=$GITLAB_PRIVATE_TOKEN"
在这里,使用“namespace.name”作为组的名称空间

该脚本基本上执行以下操作:

获取与项目匹配的所有项目\u搜索\u参数 获取他们的路径和ssh\u url\u到\u repo

  • 如果目录路径存在,请将cd放入其中并调用git pull

  • 如果目录路径不存在,请调用git clone

  • 我的frined为此构建了一个脚本(需要curl、git、jq)。我们使用它,它工作得很好:

    要查找名称空间,最好快速查看API:

    curl "https://example.com/api/v3/projects?private_token=$GITLAB_PRIVATE_TOKEN"
    
    在这里,使用“namespace.name”作为组的名称空间

    该脚本基本上执行以下操作:

    获取与项目匹配的所有项目\u搜索\u参数 获取他们的路径和ssh\u url\u到\u repo

  • 如果目录路径存在,请将cd放入其中并调用git pull

  • 如果目录路径不存在,请调用git clone


  • 按filename.sh保存以下文件 -开放式终端 -转到文件保存目录 -键入“shfilename.sh”命令并点击回车键

    在这里,所有来自本地服务器的项目都通过使用此shell脚本移动到GitLab服务器

    # Below is project directory where we are clone all projects
    cd /Users/abcd.pqrs/Documents/dev/AllProjects
    
    #Private Tocken
    declare projectTocken="YourProjectTockenString"
    declare namespaceId=111 #(Your NameSpceId from GitLab)
    
    # Source Repo Array
    declare -a source_urls=(
    "git@source_Server_IP:/home/git/project_code/projects/project1.git"
    "git@source_Server_IP:/home/git/project_code/projects/project2.git"
    "git@source_Server_IP:/home/git/project_code/projects/project3.git"
    )
    
    # Project Names by using *Source Repo* URL Array
    declare -a project_names=(
    "project1"
    "project2"
    "project3"
    )
    
    # *Destination Repo* URLs
    declare -a target_urls=(
    "http://destination_Server_IP/groupName/project1.git"
    "http://destination_Server_IP/groupName/project1.git"
    "http://destination_Server_IP/groupName/project1.git"
    )
    
    ## now loop through the above Source URL array
    for i in "${!source_urls[@]}"
    do
    # Clone the project into local directory
    echo "Clonning  ${source_urls[$i]}"
    git clone "${source_urls[$i]}"
    
    # Go to project folder/directory
    cd "${project_names[$i]}"
    
    # Create New Project on Gitlab
    curl -H "Content-Type:application/json" http://destination_Server_IP/api/v4/projects?private_token="$projectTocken" -d "{ \"name\": \"${project_names[$i]}\", \"namespace_id\": \"$namespaceId\" }"
    
    # Set the new server URL for this new directory
    git remote set-url origin "${target_urls[$i]}"
    
    for remote in `git branch -r | grep -v /HEAD`; do
    echo "Switching to $remote Branch"
    #Track all the remote branches into local repo
    git checkout --track $remote ;
    git status
    done
    #Push All branches to new server
    git push origin --all
    
    # Move back to Mobility directory so that next repo will start with new directory in same folder
    cd -
    pwd
    done
    

    根据需要编辑此脚本。

    按filename.sh保存以下文件 -开放式终端 -转到文件保存目录 -键入“shfilename.sh”命令并点击回车键

    在这里,所有来自本地服务器的项目都通过使用此shell脚本移动到GitLab服务器

    # Below is project directory where we are clone all projects
    cd /Users/abcd.pqrs/Documents/dev/AllProjects
    
    #Private Tocken
    declare projectTocken="YourProjectTockenString"
    declare namespaceId=111 #(Your NameSpceId from GitLab)
    
    # Source Repo Array
    declare -a source_urls=(
    "git@source_Server_IP:/home/git/project_code/projects/project1.git"
    "git@source_Server_IP:/home/git/project_code/projects/project2.git"
    "git@source_Server_IP:/home/git/project_code/projects/project3.git"
    )
    
    # Project Names by using *Source Repo* URL Array
    declare -a project_names=(
    "project1"
    "project2"
    "project3"
    )
    
    # *Destination Repo* URLs
    declare -a target_urls=(
    "http://destination_Server_IP/groupName/project1.git"
    "http://destination_Server_IP/groupName/project1.git"
    "http://destination_Server_IP/groupName/project1.git"
    )
    
    ## now loop through the above Source URL array
    for i in "${!source_urls[@]}"
    do
    # Clone the project into local directory
    echo "Clonning  ${source_urls[$i]}"
    git clone "${source_urls[$i]}"
    
    # Go to project folder/directory
    cd "${project_names[$i]}"
    
    # Create New Project on Gitlab
    curl -H "Content-Type:application/json" http://destination_Server_IP/api/v4/projects?private_token="$projectTocken" -d "{ \"name\": \"${project_names[$i]}\", \"namespace_id\": \"$namespaceId\" }"
    
    # Set the new server URL for this new directory
    git remote set-url origin "${target_urls[$i]}"
    
    for remote in `git branch -r | grep -v /HEAD`; do
    echo "Switching to $remote Branch"
    #Track all the remote branches into local repo
    git checkout --track $remote ;
    git status
    done
    #Push All branches to new server
    git push origin --all
    
    # Move back to Mobility directory so that next repo will start with new directory in same folder
    cd -
    pwd
    done
    

    根据需要编辑此脚本。

    git push--all
    应负责“所有分支和标记”requirement@ohlec同意。但我需要将所有项目克隆到本地目录(从本地服务器),然后需要在gitlab服务器上传输克隆的项目。
    git push--all
    应该负责“所有分支和标记”requirement@ohlec同意。但我需要将所有项目克隆到本地目录(从本地服务器),然后需要在gitlab服务器上传输克隆的项目。