How to Move a Git Repository Without Losing History
  1. Moving or duplicate branches
  2. Moving or duplicating a whole repository
    1. One time duplicating
    2. Mirroring a repository and periodically push the changes with git clone –mirror
  3. References

[Updated 2025-March]

Moving or duplicate branches

(In this example, we use the command line. If you are using a GUI tool e.g SourceTree,…we can achieve the same thing)

For example: We want to move the “main” branch into the new repository.

Step 1: Checkout the source branch

git checkout main

Step 2: Pull the latest code from the source branch

Make sure the latest codes are committed and pushed to the remote branch.

git pull

Step 3: Add a remote to the destination repository

git remote add origin <url to the repo>

Step 4: Push the source branch to the remote repository

git push -u origin main

Step 5: Done

Moving or duplicating a whole repository

For example: We want to move everything:

  • Branches
  • Tags

One time duplicating

Step 1: (Optional) CHECKOUT EVERYTHING FROM THE OLD REPOSITORY

This is an optional step as we can use the cloned old Git repository that we are working.

However, It’s recommended to clone the mirror to new newly recreated folder/location.

So that we won’t push a local branch in your local repository.

Make sure all codes are committed and pushed to the remote branch.

# Clone the old repository to the `my-old-repo` directory
git clone --bare <url to OLD repo> ./my-old-repo

Notes:

  • We can also use git clone –mirror . The different is that:
    • The clone bare: only down load all administrative files into .git folder, there is no checkout, no remote-tracking branches are created
    • The clone mirror: the same as bare, with extra step:
      • It maps local branches of the source to local branches of the target, It maps all refs including remote-tracking branches, notes etc..

Step 2: PUSH EVERYTHING TO THE NEW REPOSITORY

# Change working directory to `my-old-repo`
cd ./my-old-repo

# Push everything to the remote NEW repository
git push --mirror <url to NEW repo>

Step 3: Done.

Now everything has been pushed to the new repository.

You can clone the code from the new repository now.

Mirroring a repository and periodically push the changes with git clone –mirror

Phase 1: Mirroring a repository

You can skip this step if you have already mirrored the repository before.

Follow the same approach as “One time duplicating” section to mirror the repository.

Phase 2: Periodically push the changes from the OLD to the NEW

Step 1: Pull the latest changes from the OLD

# Change working directory to `my-old-repo`
cd ./my-old-repo

# Pull everything from the remote
git fetch -p origin 

Where:

  • -p or –prune: Before fetching, remove any remote-tracking references that no longer exist on the remote.

Step 2: Push the latest changes to the NEW

# From `my-old-repo`
# Push everything to the remote NEW repository
git push --mirror <url to NEW repo>

Step 3: Done

References

[1] https://docs.github.com/en/repositories/creating-and-managing-repositories/duplicating-a-repository#mirroring-a-repository-in-another-location

[2] https://www.atlassian.com/git/tutorials/git-move-repository

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I’m Hung

Welcome to my personal space. This is a small corner where I share ideas and things I find interesting,..

Let’s connect

Don’t hesitate to reach out if there’s anything we can discuss, or simply to connect :)