Recently I came across the task of migrating existing repositories from one gitlabs instance to another.

I could have generated a backup and restored it, however, I didn’t want to deal with any migration issues so I decided to simple push the existing repositories to a new remote.

Below is a shell scripts that clones existing git repositories and then push their refs to a new remote:


 #!/bin/bash

old_name[0]="old-repo-name"  
 old_name[1]="same-repo-name"  
 new_name[0]="new-repo-name"  
 new_name[1]="same-repo-name"

cur_dir=$(pwd)  
 for (( i = 0 ; i < ${#old_name[@]} ; i=$i+1 ));  
 do  
 echo -e "nnRepo $i"  
 old=${old_name[${i}]}  
 new=${new_name[${i}]}  
 echo "old – $old"  
 echo "new – $new"

 if [ ! -d "$old" ]; then  
 echo -e "nCloning repository $old…"

 git clone git@gitlabs.dev.cloud:clouddynamics/$old.git  
 cd $old  
 git remote rm origin  
 git remote add origin git@gitlabs.cloud:cloud-dynamics/$new.git  
 git push –all origin  
 cd $cur_dir  
 fi  
 done  

**UPDATE

The script above will not push all branches to the new remote.
If you want to migrate all branches run the following script as well:

  
 #!/bin/bash

old_name[0]="repo-name"  
 old_name[1]="another-repo-name"

cur_dir=$(pwd)  
 for (( i = 0 ; i < ${#old_name[@]} ; i=$i+1 ));  
 do  
 echo -e "nnRepo $i"  
 old=${old_name[${i}]}  
 echo "old – $old"  
 cd $old  
 echo -e "nPushing branches from old to origin remote…"  
 git remote add old git@gitlabs.dev.cloud:clouddynamics/$old.git  
 git fetch old  
 git push origin refs/remotes/old/*:refs/heads/*  
 cd $cur_dir  
 done