How to Migrate a Git Repository While Preserving Full Commit History

Summary
A step-by-step guide to migrate Git repositories between platforms like GitHub, GitLab, and Bitbucket while preserving all commit history, branches, and tags using bare clone.

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).

Do not check Add a README file, Add .gitignore, and Choose a license. The repository must be completely empty to ensure a smooth migration.

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.

1
git clone https://gitlab.com/abc/xyz.git --bare

What is a bare repository? A bare repository contains only Git’s version control data (the .git folder 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:

1
2
cd xyz.git
git push https://github.com/abc/xyz.git --all

Push All Tags to the Destination

Transfer all tags from the local repository to the destination platform:

1
git push https://github.com/abc/xyz.git --tags

Verification

After migration, verify your repository by:

  1. Check commit history: git log should show all previous commits
  2. Check branches: git branch -a should list all branches
  3. Check tags: git tag should show all version tags

One-Liner Script

For convenience, here’s a complete migration script:

1
2
3
4
5
6
7
8
# Replace SOURCE_URL and DEST_URL with your repository URLs
SOURCE_URL="https://gitlab.com/abc/xyz.git"
DEST_URL="https://github.com/abc/xyz.git"

git clone $SOURCE_URL --bare && \
cd $(basename $SOURCE_URL) && \
git push $DEST_URL --all && \
git push $DEST_URL --tags

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.