How to Migrate a Git Repository While Preserving Full Commit History
Objective
This guide demonstrates how to seamlessly migrate a code repository between different platforms while preserving the complete commit history. The process is applicable to any Git-based repository platforms, such as GitHub, GitLab, or Bitbucket.
Steps
To illustrate the migration process, we’ll walk through moving a repository from GitLab (source platform) to GitHub (destination platform).
Create a New Project on the Destination Platform
First, create a blank repository on GitHub using the same name as your source repository (though using a different name is also acceptable).
Clone the Repository as Bare
Create a mirror copy of the source repository on your local machine using the --bare flag. This specialized clone operation creates a repository that contains all the version control data—including branches, tags, and the complete commit history—but without a working directory for file editing.
| |
What is a bare repository? A bare repository contains only Git’s version control data (the
.gitfolder contents) without the actual working files. It’s ideal for migration and server-side repositories.
Push All Branches to the Destination
Navigate to the cloned bare repository and push all branches and objects to the destination platform:
Push All Tags to the Destination
Transfer all tags from the local repository to the destination platform:
| |
Verification
After migration, verify your repository by:
- Check commit history:
git logshould show all previous commits - Check branches:
git branch -ashould list all branches - Check tags:
git tagshould show all version tags
One-Liner Script
For convenience, here’s a complete migration script:
Conclusion
Your repository has been successfully migrated with all commit history, branches, and tags fully preserved. The new repository is ready for use while maintaining its complete development history.