Skip to content

Instantly share code, notes, and snippets.

@dbirks
Last active September 11, 2023 06:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save dbirks/ed249df1912ec11214327a06e24d816c to your computer and use it in GitHub Desktop.
Save dbirks/ed249df1912ec11214327a06e24d816c 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
@aslamanver
Copy link

aslamanver commented Dec 28, 2022

Use GitHub CLI

gh repo list orgname --limit 1000 | while read -r repo _; do
  echo "git clone --mirror git@github.com:$repo.git"
  git clone --mirror git@github.com:$repo.git
  # cd $repo.git
  # git remote set-url origin git@github.com:orgname/$REPO.git
  # git push --mirror
done

@abilly01
Copy link

Use GitHub CLI

gh repo list payable --limit 1000 | while read -r repo _; do
  echo "git clone --mirror git@github.com:$repo.git"
  git clone --mirror git@github.com:$repo.git
  # cd $repo.git
  # git remote set-url origin git@github.com:orgname/$REPO.git
  # git push --mirror
done

Thanks for this base. It helped me a LOT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment