How to Transfer Git Repository While Keeping Commit History

📝 Sharing my journey as an aspiring web developer and documenting for my future self
🌱 Currently learning JavaScript
Disclaimer: This approach worked for me, but it may not necessarily be the best technique
Step 1: Create Mirror Clone of Old Repository
git clone --mirror - Makes a total copy of the original repo. This includes every reference: remote-tracking branches, notes. Remote tracking is also set up so that if git remote update is run, all refs will be overwritten from origin, ie: a functionally identical copy of original repo
old-repo-url - Replace with Git URL of old repo
new-repo - Replace with the name of your new directory.
git clone --mirror old-repo-url new-repo
Step 2: Remove Remote Reference to Old Repository
cd new-repo
git remote remove origin
Step 3: Add Remote Reference to New Repository
new-repo-url - Replace with Git URL of your new repo
git remote add origin new-repo-url
Step 4: Push All References to New Repository
git push --all
git push --tags
Step 5: Clone New Repository
rm -rf new-repo - Deletes mirrored cloned repo from Step 1 as it can't be used for new commits because we cloned it from the old-repo-url
new-repo-url, new-repo - Replace with new repo URL and name
cd ..
rm -rf new-repo
git clone new-repo-url new-repo
Congrats! 👏🏻👏🏻👏🏻
You now have a complete clone of the original repo with all the commit history!
Illustration by Murat Kalkavan from Icons8
See Also
- https://www.git-scm.com/docs/git-clone
- https://stackoverflow.com/questions/3959924/whats-the-difference-between-git-clone-mirror-and-git-clone-bare
- https://itnext.io/git-repository-transfer-keeping-all-history-670fe04cd5e4?gi=bf7b9a1c6dcb



