How to move git repository with all branches from bitbucket to github?

前端 未结 11 1785
孤城傲影
孤城傲影 2020-12-02 03:21

What is the best way to move a git repository with all branches and full history from bitbucket to github? Is there a script or a list of commands I have to use?

11条回答
  •  天涯浪人
    2020-12-02 03:59

    I made the following bash script in order to clone ALL of my Bitbucket (user) repositories to GitHub as private repositories.


    Requirements:

    • jq (command-line JSON processor) | MacOS: brew install jq

    Steps:

    1. Go to https://github.com/settings/tokens and create an access token. We only need the "repo" scope.

    2. Save the move_me.sh script in a working folder and edit the file as needed.

    3. Don't forget to CHMOD 755

    4. Run! ./move_me.sh

    5. Enjoy the time you have saved.


    Notes:

    • It will clone the BitBucket repositories inside the directory the script resides (your working directory.)

    • This script does not delete your BitBucket repositories.


    Need to move to public repositories on GitHub?

    Find and change the "private": true to "private": false below.

    Moving an organization's repositories?

    Checkout the developer guide, it's a couple of edits away.


    Happy moving.

    #!/bin/bash
    
    BB_USERNAME=your_bitbucket_username 
    BB_PASSWORD=your_bitbucket_password
    
    GH_USERNAME=your_github_username
    GH_ACCESS_TOKEN=your_github_access_token
    
    ###########################
    
    pagelen=$(curl -s -u $BB_USERNAME:$BB_PASSWORD https://api.bitbucket.org/2.0/repositories/$BB_USERNAME | jq -r '.pagelen')
    
    echo "Total number of pages: $pagelen"
    
    hr () {
      printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -  
    }
    
    i=1
    
    while [ $i -le $pagelen ]
    do
      echo
      echo "* Processing Page: $i..."
      hr  
      pageval=$(curl -s -u $BB_USERNAME:$BB_PASSWORD https://api.bitbucket.org/2.0/repositories/$BB_USERNAME?page=$i)
      
      next=$(echo $pageval | jq -r '.next')
      slugs=($(echo $pageval | jq -r '.values[] | .slug'))
      repos=($(echo $pageval | jq -r '.values[] | .links.clone[1].href'))
      
      j=0
      for repo in ${repos[@]}
      do
        echo "$(($j + 1)) = ${repos[$j]}"
        slug=${slugs[$j]}
      git clone --bare $repo 
      cd "$slug.git"
      echo
      echo "* $repo cloned, now creating $slug on github..."  
      echo  
    
      read -r -d '' PAYLOAD <

提交回复
热议问题