Sunday, 8 April 2018

Move source code from one repository to another in GitHub

Tip of the Week - How to move a full Git repository.

January 22nd 2016  Peter Van de Voorde in Tip of the WeekGit

This week I'll show you how you can move a full Git repository from one remote server to another. The steps I'm using even allow you to choose which branches and tags to include.
Let’s call the original repository ORI and the new one NEW, here are the steps I took to copy everything from ORI to NEW:
  1. Create a local repository in the temp-dir directory using:
    1
    git clone <url to ORI repo> temp-dir
    Git clone original repository into local temp-dir
  2. Go into the temp-dir directory.
  3. To see a list of the different branches in ORI do:
    1
    git branch -a
    See all the Branches
  4. Checkout all the branches that you want to copy from ORI to NEW using:
    1
    git checkout branch-name
    Checkout all branches
  5. Now fetch all the tags from ORI using:
    1
    git fetch --tags
    Fetch all the tags
  6. Before doing the next step make sure to check your local tags and branches using the following commands:
    1
    git tag
    2
    git branch -a
    Git tag and Git branch to check if we have everything we need.
  7. Now clear the link to the ORI repository with the following command:
    1
    git remote rm origin
  8. Now link your local repository to your newly created NEW repository using the following command:
    1
    git remote add origin <url to NEW repo>
  9. Now push all your branches and tags with these commands:
    1
    git push origin --all
    2
    git push --tags
    The End Result of our 10 steps.
  10. You now have a full copy from your ORI repo.

Extra:

If you want to simply copy the entire repository you can use
1
git clone --mirror <url to ORI repo> temp-dir
to replace step 1 to 5.