Last active
May 3, 2020 20:50
-
-
Save yangsu/edc9fb5b7f704ca2ef5d6410317ea00b to your computer and use it in GitHub Desktop.
Migrate repo from BitBucket to Github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: | |
# GITHUB_TOKEN=... ./bitbucket-to-github.sh <bitbucket_username> <bitbucket_repo> [github_username] [github_repo] | |
BITBUCKET_USERNAME="$1" | |
BITBUCKET_REPO="$2" | |
GITHUB_USERNAME="${3:-$1}" | |
GITHUB_REPO="${4:-$2}" | |
[ $# -lt 2 ] && echo "Pass in at least your bitbucket username and repo." && exit | |
[ -z "$GITHUB_TOKEN" ] && echo "Create a new token with repo scope at https://github.com/settings/tokens/new?scopes=repo and pass it in as GITHUB_TOKEN" && exit | |
echo "Moving repo from $BITBUCKET_USERNAME/$BITBUCKET_REPO (bitbucket) to $GITHUB_USERNAME/$GITHUB_REPO (github)" | |
echo "1. Cloning $BITBUCKET_USERNAME/$BITBUCKET_REPO..." | |
git clone [email protected]:$BITBUCKET_USERNAME/$BITBUCKET_REPO.git | |
echo "2. Creating $GITHUB_USERNAME/$GITHUB_REPO on github..." | |
BODY=$(cat <<EOF | |
{ | |
"name": "$GITHUB_REPO", | |
"private": true | |
} | |
EOF | |
) | |
echo $BODY | |
curl -s --location --request POST 'https://api.github.com/user/repos' \ | |
--header 'Content-Type: application/json' \ | |
--header "Authorization: Bearer $GITHUB_TOKEN" \ | |
--data-raw "$BODY" | |
echo "3. Uploading to $GITHUB_USERNAME/$GITHUB_REPO" | |
(cd $BITBUCKET_REPO && \ | |
git remote set-url origin [email protected]:$GITHUB_USERNAME/$GITHUB_REPO.git && \ | |
git push origin --all && git push origin --tags) | |
echo "Done! See repo in https://github.com/$GITHUB_USERNAME/$GITHUB_REPO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment