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 into a temporary directory
git clone <url-to-ORI-repo> temp-dir
2. Navigate into the new directory
cd temp-dir
3. List all branches in the original repository
git branch -a
4. Checkout the branches you want to copy
git checkout branch-name
5. Fetch all tags from the original repository
git fetch --tags
6. Verify the branches and tags have been fetched correctly
git tag
git branch -a
7. Remove the link to the original repository
git remote rm origin
8. Add the new repository as the origin
git remote add origin <url to NEW repo>
9. Push all branches and tags to the new repository
git push origin --all
git push origin --tags
10. Done!
You now have a complete copy of the original repository in your new remote.
Alternative: Use git clone --mirror
For a complete migration, including all refs and branches, you can clone with the --mirror option:
git clone --mirror <url to ORI repo> temp-dir
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