Skip to content

Instantly share code, notes, and snippets.

@joshjohanning
Forked from dbirks/migrate-git-repos.md
Created October 5, 2022 16:10
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 joshjohanning/afa0603ae96862bfec02745cba032494 to your computer and use it in GitHub Desktop.
Save joshjohanning/afa0603ae96862bfec02745cba032494 to your computer and use it in GitHub Desktop.
Script to fully migrate git repos, including all tags and branches

Migrate git repos script

I used this for migrating git repos from Bitbucket to Github. It uses git's --mirror flag for cloning and pushing to also transfer all tags and branches.

It would be helpful to have SSH keys set up on both ends. Then all you should have to do is to make sure the hardcoded orgname is set to the appropriate one for both the source and destination.

Once I migrated repos, I used this to replace my origin url locally (assumes using ssh):

sed -i s/bitbucket.org:orgname/github.com:orgname/g .git/config

(Optional) Mass migration

To do this in mass, I put each repo name on its own line in a file named repo-list, and looped through them with:

for repo in $(cat repo-list); do ./migrate-git-repos.sh $repo; done

Happy migrating! 🐦

#!/bin/bash
# Usage:
# Create the repo in Github, and then give the repo name as the argument:
# ./migrate-git-repos.sh repo-name
# Echo commands as they're run, and expand variables
set -o xtrace
REPO="$1"
git clone --mirror git@bitbucket.org:orgname/$REPO.git
cd $REPO.git
git remote set-url origin git@github.com:orgname/$REPO.git
git push --mirror
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment