Skip to content

Instantly share code, notes, and snippets.

@rajiff
Last active May 11, 2022 13:45
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 rajiff/bc6da3f4804eb4795d04bbe1d6dae615 to your computer and use it in GitHub Desktop.
Save rajiff/bc6da3f4804eb4795d04bbe1d6dae615 to your computer and use it in GitHub Desktop.
Shell script to migrate a repo from one server to another along with branches & commit history
#!/bin/sh
# ensure correct number of arguments are passed
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <SOURCE Repo URL> <DIR to clone to> <TARGET Repo URL>" >&2
echo "example: /.migrate_git_repo.sh git@git.example.com:sampleuser/source_repo.git some_folder_name ssh://git@othergit.example.com:otheruser/destination_repo.git"
return
fi
srcRepo=$1
cloneToDir=$2
targetRepo=$3
echo "\n\n"
echo About migrate $srcRepo to $targetRepo via the folder $cloneToDir
# clone to local folder, clone and checkout all branches of the repo
git clone --mirror $srcRepo
cd ./$cloneToDir.git
# fetch everything to local
git fetch --tags
git fetch -a
git tag
# this should list all the branches, check none are red (red indicate its not checkedout locally and probably will be missed)
git branch -a
# move origin from source to target
git remote rm origin
git remote add origin $targetRepo
# push all local repo refs to target
git push origin --all
git push --tags
# clean up the cloned folder
#rm -rf ./$cloneToDir
# Get back to where the script was triggerred from
cd -
echo Done migrating $srcRepo to $targetRepo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment