1

I want to migrate an old Bitbucket repository, containing multiple branches, to Github. To accomplish this, I've followed this description:

# add remote github repository
$ git remote add upstream https://github.com:USER/PROJECT.git
# push all branches <-- doesn't work
$ git push upstream master
$ git push --tags upstream

Only the master branch is pushed to the Github repository (also for git push --all upstream).

In order to push all branches to Github, I need to checkout them individually and do git push upstream.

How can I push all branches to Github without first checkout them?

Shuzheng
  • 11,288
  • 20
  • 88
  • 186

4 Answers4

4

Follow the below steps.

  1. Mirror the source repo.

    git clone --mirror https://url-of-the-source-git-repo.git

  2. Go to the newly cloned repo

    cd to-your-git-repo-folder.git

  3. set the new remote url.

    git remote set-url --push origin https://url-of-the-destination-git-repo.git

  4. Push to the new repo

    git push --mirror

By following these commands you will migrate to new repo with all the branches and commits.

Chandan Kumar
  • 1,066
  • 10
  • 16
  • Thank you very much! This seems to be working. Since I'm new to Git, does this make a "hard" copy of the Bitbucket repository on Github, or does it create a "soft" copy, where only references are copied, such that the Github repository references the Bitbucket repository, but doesn't actually contains the files itself? – Shuzheng Nov 12 '19 at 09:50
  • This is going to copy the actual files not the references, you can start working on new repository after migration. – Chandan Kumar Nov 12 '19 at 10:54
1

You can do it by using this single command.

git push REMOTE --mirror

You can read more about it here

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
0

First make sure that you have fetched all branches.

git fetch --all

Then push all branches to the new remote

git push upstream --all
Tom
  • 4,070
  • 4
  • 22
  • 50
  • It still just push a single branch (`master`). It should be noted that I've never worked on the other branches locally before. It's an old Bitbucket repository that I cloned recently. – Shuzheng Nov 11 '19 at 12:54
0

Just a note for anyone stumbling across this that the accepted answer only changes the remote for git push. Thus, when you fetch or pull, it will still be referencing the original repository on BitBucket.

In order to avoid this, simply drop the --push when setting the remote url:

git remote set-url origin git@github.com:org/my_repository.git

For the full process:

git clone --mirror git@bitbucket.org:org/my_bitbucket_repository.git

cd my_bitbucket_repository.git

git remote set-url origin git@github.com:org/my_github_repository.git

git push --mirror

You can then verify this has actually changed the remote by:

git remote show origin

Which will output something similar to this:

  Fetch URL: git@github.com:org/my_github_repository.git
  Push  URL: git@github.com:org/my_github_repository.git
  HEAD branch: main
  Remote branches:
    main    tracked
  Local refs will be mirrored by 'git push' 
A. Johnson
  • 23
  • 3