Skip to content

Instantly share code, notes, and snippets.

@woolfg
Created April 21, 2020 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woolfg/b4f36b8ba0c351d11712121ea858d771 to your computer and use it in GitHub Desktop.
Save woolfg/b4f36b8ba0c351d11712121ea858d771 to your computer and use it in GitHub Desktop.
Script to migrate / move a git repository (incl. tags and branches) to new remote destination
#!/bin/bash
#
# Script migrates git repos incl. branches and tags
# from an old remote host to a new one
#
# Specify one old and new remote destination per line
# and separate them with a space
repos=(
"git@git.inlupus.at:inlupus_servers/ansible_config.git git@github.com:inlupus/ansible_config.git"
);
for i in "${repos[@]}"; do
from=$(echo $i | cut -d ' ' -f 1)
to=$(echo $i | cut -d ' ' -f 2)
tmpfolder=$(mktemp -d)
echo "Migrating ${from} to ${to}"
git clone ${from} ${tmpfolder}
cd ${tmpfolder}
git fetch -a
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
git pull --all
git remote add new-origin ${to}
git push --all new-origin
git push --tags new-origin
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment