SuvaP
SuvaP

Reputation: 39

How to transfer existing git repo from local server to gitlab repo, using CLI(Shell script)?

I have many git repos on my server. Using command line(script) I want to make new gitlab repos on https://gitlab.companyname.com, form existing local repos. And also push all branches with tags on gitLab server.

Is it possible to do that without enterprise account?

Upvotes: 0

Views: 568

Answers (2)

SuvaP
SuvaP

Reputation: 39

Save below file by filename.sh - open terminal - goto file saved directory - type "sh filename.sh" command and hit enter

Here all the projects from local server are moved to your GitLab server by using this shell script.

# 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

Edit this script as per requirement.

Upvotes: 2

gustavovelascoh
gustavovelascoh

Reputation: 1228

You can add your gitlab repo as a remote in your server. Then you can use push --all.

Something like this in each repo directory you have:

git add remote gitlab <YOUR NEW REPO URL>
git push gitlab --all

Upvotes: 0

Related Questions