Migrate git repositories

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 "\n\nRepo $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 scripts 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 "\n\nRepo $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
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s