Skip to content

Instantly share code, notes, and snippets.

@jasonwarta
Last active January 6, 2019 03:10
Show Gist options
  • Save jasonwarta/523cb4c696ade76679c5d476ccb3b058 to your computer and use it in GitHub Desktop.
Save jasonwarta/523cb4c696ade76679c5d476ccb3b058 to your computer and use it in GitHub Desktop.
script for migrating a git repo between services
# put this function in some file that will be loaded into your bash profile
# I personally use a dedicated sourced file for functions but it could also be placed
# in your .bashrc, .profile, or .bash_profile
# Usage:
# at a bash prompt, run
# migrate-repo <repo-url>
# the repo url could look like [email protected]:username/repo-name.git
function migrate-repo() {
new_repo_url=$1
NAME=new_repo_`date +%s`
if [ -z $1 ]; then
echo "You must provide a target url for the repo"
exit
fi
# this loop grabs all the branches from the (old) remote
# it uses sed to prune the list of branches, which may cause
# issues for some users with nonstandard sed installs (such as mac)
for remote in `git branch -r|sed 's,origin/\|->\|HEAD,,g'`;
do
git checkout -b $remote
done
git remote add $NAME $new_repo_url
git push -u $NAME --all
git push -u $NAME --tags
git remote rm origin
git push $NAME master
git remote rename $NAME origin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment