Skip to content

Instantly share code, notes, and snippets.

@chinmaya-n
Last active August 9, 2023 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chinmaya-n/cff02f1277c811deab2e550f2aad9967 to your computer and use it in GitHub Desktop.
Save chinmaya-n/cff02f1277c811deab2e550f2aad9967 to your computer and use it in GitHub Desktop.
Migrate repos from Bitbucket to Github (only Master branch)
#!/bin/bash
# verify for two script params
if [ "$#" -ne 2 ]; then
echo "Need two arguments."
echo "Usage: ./bitbucket_to_github.sh <repo_name> <bitbucket_url>"
echo ""
echo "Prerequisits:"
echo "* Public Key authentication for both Github & Bitbucket already setup."
echo "* Update this script with your Github Username & API Token."
exit 1
fi
USERNAME=github_user_name
API_TOKEN=489_github_api_token_5268
REPO_NAME=$1
BB_URL=$2
echo ""
echo "Cloning the repo ..."
git clone --bare $BB_URL
pushd $REPO_NAME
git remote rename origin bitbucket
echo ""
echo "Create repo: $REPO_NAME in github ..."
curl --request POST \
--url https://api.github.com/user/repos \
--user $USERNAME:$API_TOKEN \
--header 'cache-control: no-cache' \
--header 'content-type: application/json' \
--data '{"name": "'"$REPO_NAME"'", "private": true}'
echo ""
echo "Pushing to Github ..."
git remote add origin git@github.com:$USERNAME/$REPO_NAME.git
git push -f origin master
git remote rm bitbucket
popd
@chinmaya-n
Copy link
Author

Technically not just bitbucket, the repo URL can be any Git server. Feel free to modify this script to make it better. One can also eliminate the repo name input as well, by extracting it from the repo URL.

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