DEV Community

Babar Bilal
Babar Bilal

Posted on

Migrate Your Bitbucket Repository to GitHub Effortlessly

Image description

Introduction

Migrating repositories from Bitbucket to GitHub can be a daunting task, especially when dealing with multiple branches, commits, and tags. To simplify this process, I have created an automated script that efficiently migrates your Bitbucket repository to GitHub while ensuring all branches, commits, and tags remain intact.

This guide will walk you through the process and help you troubleshoot common migration issues.

Why Migrate to GitHub?

GitHub has become the go-to platform for developers due to its vast ecosystem, better integrations, and superior collaboration tools. Whether you're moving due to Atlassian's strategic changes or just prefer GitHub's ecosystem, this script will help you make a seamless transition.

Prerequisites

Before running the script, ensure that you have the following:

  • A Bitbucket repository that you want to migrate.
  • A GitHub account and a personal access token with repo permissions.
  • Git installed on your system.
  • Curl for making API requests (installed by default on most systems).

How to Use the Migration Script

Step 1: Clone the Migration Script

Download or clone the script from the GitHub repository:

git clone https://github.com/babarbilal56/bitbucket-to-github-repo-migration.git
cd bitbucket-to-github-repo-migration
Enter fullscreen mode Exit fullscreen mode

Step 2: Run the Script

Execute the script with the following command:

./migrate_repo.sh <BITBUCKET_REPO_URL> <GITHUB_REPO_URL> <GITHUB_ACCESS_TOKEN> <BITBUCKET_USERNAME> <GITHUB_USERNAME>
Enter fullscreen mode Exit fullscreen mode

Example:

./migrate_repo.sh https://bitbucket.org/user/repo.git https://github.com/user/repo.git ghp_xxxxx bitbucket_user github_user
Enter fullscreen mode Exit fullscreen mode

Script Workflow

Clone the Bitbucket Repository

The script clones the repository, including all branches and commits, using:

git clone --bare <BITBUCKET_REPO_URL>
Enter fullscreen mode Exit fullscreen mode

Check if GitHub Repository Exists

  • If the repository exists, it skips the creation process.
  • If not, it creates a new repository using the GitHub API.

Set Up GitHub Remote

The script adds GitHub as the new remote:

git remote add github <GITHUB_REPO_URL>
Enter fullscreen mode Exit fullscreen mode

Push to GitHub

It pushes all branches, commits, and tags using:

git push --mirror
Enter fullscreen mode Exit fullscreen mode

Clean Up

After migration, it deletes the local repository clone to save space.

Troubleshooting Common Issues

1. GitHub Push Errors

If you encounter an error like:

fatal: RPC failed; curl 55 Recv failure: Connection reset by peer
Enter fullscreen mode Exit fullscreen mode

Try the following solutions:

Increase Git Buffer Size:

git config --global http.postBuffer 524288000
Enter fullscreen mode Exit fullscreen mode

Use SSH Instead of HTTPS:

git remote set-url origin git@github.com:<GITHUB_USERNAME>/<GITHUB_REPO>.git
git push --mirror
Enter fullscreen mode Exit fullscreen mode

Increase Timeout Settings:

git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
Enter fullscreen mode Exit fullscreen mode

2. GitHub Repository Not Created

  • Ensure that your GitHub token has the correct permissions (repo scope).
  • Check GitHub API responses for errors.
  • Manually verify the repository's existence by visiting:
  https://github.com/<GITHUB_USERNAME>/<GITHUB_REPO>
Enter fullscreen mode Exit fullscreen mode

3. Push Fails for Large Files

If your repository has large files, consider using Git Large File Storage (LFS):

git lfs install
git lfs track "*.largefile"
git add .gitattributes
Enter fullscreen mode Exit fullscreen mode

Enhancing the Script

You can customize the script further by:

  • Changing repository visibility from private to public.
  • Handling multiple branches by pushing them separately instead of using --mirror.

Conclusion

This script provides an efficient way to migrate your Bitbucket repository to GitHub, maintaining all branches, commits, and tags. If you encounter issues, follow the troubleshooting steps outlined above.

For further assistance or contributions, check out the GitHub repository:

Happy Coding! πŸš€

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay