Git: Add multiple repositories(Bitbucket, Github) to the same project.

12 / Mar / 2023 by Dheeraj Gupta 0 comments

In this blog, we are going to learn about an important feature (migration across repositories providers) of one of the most common technologies we came across in our day-to-day development, GIT.

I mainly used to do R&D work on my private repositories, on bitbucket, as it supports unlimited private repositories space. But after some time, when I started contributing to open source, I realized that GitHub is better at it and I want to use some of my already done work. Therefore, I need to migrate some of my repositories to bitbucket.

Prerequisites: a Bitbucket repository (where original code is present) and a GitHub repository(target repository where the code is to be moved).

Let’s see how I achieve it:

Step 1:  Clone the code from bitbucket to your local system using the git clone command.

git clone https://dheerajgupta217@bitbucket.org/dheerajgupta217/bitbucketrepo.

Output:

git
Cloning into ‘bitbucketrepo’…
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 13 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (13/13), 1.96 KiB | 49.00 KiB/s, done.

Step 2:  Move to the location where the project is cloned

 cd bitbucketrepo/

Step 3:  Add the secondary repository origin with a different name (I used github for simplicity reasons)

git remote add github https://github.com/dheerajgupta217/secondary_repository.

Step 4:  Move your master to the new repository

 git push github master

Output:

Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 1.98 KiB | 1015.00 KiB/s, done.
Total 13 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/dheerajgupta217/secondary_repository.git
* [new branch] master -> master

Step 5:  Set the URL of the new repository to the origin added.

 git remote set-url github https://github.com/dheerajgupta217/secondary_repository

Step 6:  Check if both repositories are working.

git branch -a

Output:

* master
remotes/github/master
remotes/origin/HEAD -> origin/master
remotes/origin/master

Github repo

Voila, the git migration is done

Testing the migration activity:

Step 1: Add a new branch to the bitbucket and push it.

git checkout -b test_branch
git push origin test_branch

Step 2: Push the same branch to GitHub now.

git push github test_branch

Output:

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'test_branch' on GitHub by visiting:
remote: https://github.com/dheerajgupta217/secondary_repository/pull/new/test_branch
remote:
To https://github.com/dheerajgupta217/secondary_repository
* [new branch] test_branch -> test_branch

The branch and the code must be created in GitHub.

Use Case:

Recently for one of our clients, this use case is being used where the code is needed to be moved to code-commit form bitbucket and the client requires his old code as well intact. Migration for it was achieved using the below-mentioned steps.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *