git - 如何使用 CLI(Shell 脚本)将现有的 git 存储库从本地服务器传输到 gitlab 存储库?
问题描述
我的服务器上有很多 git 存储库。使用命令行(脚本)我想在https://gitlab.companyname.com上创建新的 gitlab 存储库,形成现有的本地存储库。并在 gitLab 服务器上推送所有带有标签的分支。
没有企业帐户可以做到这一点吗?
解决方案
通过 filename.sh 保存以下文件 - 打开终端 - 转到文件保存目录 - 键入“sh filename.sh”命令并按 Enter
在这里,使用此 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
根据要求编辑此脚本。
推荐阅读
- python-3.x - 没有文件或目录
- c# - 绑定类型“cosmosDBTrigger”未注册 + Visual Studio
- python - 是否可以使用 pandas 读取以下 txt.files?
- android - 框架布局接管屏幕并隐藏底部导航和工具栏
- apache-spark - 将子对象分组到 JSON 数组
- reactjs - 如何在 Draftjs 中实现自定义装饰器
- c - 在 C 语言中,fgets 和 gets 在 Visual Studio、Dev C++ 和 linux 上给我不同的输出?
- c# - 选择“是否要保存提示”取消按钮时,Excel-DNA 加载项会卸载
- css - 需要帮助将样式与使用过滤器进行主题化的全局设置隔离:反转
- c++ - 在 C++ 中的两个文件上使用标头?