How to Successfully Move a Git Repository Without Losing History

Migrating a Git repository can seem intimidating, but with the right process, it’s straightforward. Whether you’re switching hosting platforms, reorganizing projects, or consolidating repositories, preserving your repository’s history is crucial. A smooth migration ensures your project remains intact with all commits, branches, and tags preserved.

In this guide, we’ll walk through how to move a Git repository step-by-step while maintaining its full history.


Why Move a Git Repository?

There are several scenarios where moving a repository makes sense:

  • Changing Hosting Providers: For example, moving from GitHub to Bitbucket or GitLab.
  • Restructuring Projects: Aligning repositories to match new project structures or teams.
  • Repository Consolidation: Merging several repositories into a single unified project.

The goal is to complete the move without losing code history, branches, or tags—ensuring continuity in your development workflow.


Step-by-Step Process

1. Clone the Original Repository

Start by creating a local copy of your existing repository:

git clone temp-repo

This command pulls the entire project into a temporary directory.


2. Navigate to the Repository

Move into the cloned directory:

cd temp-repo

3. Verify All Branches

List all branches in the repository:

git branch -a

This ensures you know exactly which branches exist before migration.


4. Checkout Branches

For each branch you want to preserve, check it out:

git checkout

Repeat this step as needed.


5. Fetch All Tags

To capture all version tags, run:

git fetch –tags

6. Remove the Existing Remote

Remove the reference to the original repository:

git remote rm origin

7. Add the New Remote

Add your new repository’s URL:

git remote add origin

8. Push Everything

Push all branches and tags to the new repository:

git push origin –all git push origin –tags

This step ensures your full repository history is now hosted in the new location.


Alternative: Use git clone --mirror

For a complete migration, including all refs and branches, you can clone with the --mirror option:

git clone –mirror

Then push to the new remote as in the steps above.


Best Practices for Migration

  • Check Access Permissions: Ensure you have the correct rights for both repositories.
  • Audit Dependencies: Large projects may require special handling for Git LFS files.
  • Automate if Possible: For frequent migrations, consider scripting or integrating the process into your CI/CD pipeline.

Conclusion

Moving a Git repository while preserving history is a critical task that can be done with careful planning and execution. Following these steps ensures a smooth transition, preventing disruptions to your development workflow.

Reference: Atlassian, Git Move Repository Tutorial, https://www.atlassian.com/git/tutorials/git-move-repository