Skip to content

Instantly share code, notes, and snippets.

@roman-bgonz
Last active October 11, 2023 19:49
Show Gist options
  • Save roman-bgonz/1d945644aa5fb92644738df3216b1183 to your computer and use it in GitHub Desktop.
Save roman-bgonz/1d945644aa5fb92644738df3216b1183 to your computer and use it in GitHub Desktop.
Clone a repo preserving history
# If you're looking to preserve the existing branches and commit history, here's one way that worked for me.
git clone --mirror https://github.com/account/repo.git cloned-repo
cd cloned-repo
git push --mirror {URL of new (empty) repo}
# at this point only remote cloned-repo is correct, local has auto-generated repo structure with folders such as "branches" or "refs"
cd ..
rm -rf cloned-repo
git clone {URL of new (empty) repo}
# only now will you see the expected user-generated contents in local cloned-repo folder
# note: all non-master branches are avaialable, but git branch will not show them until you git checkout each of them
# to automatically checkout all remote branches use this loop:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
# Now, suppose you want to keep the source and destination repos in sync for a period of time. For example, there's still activity within the current remote repo that you want to bring over to the new/replacement repo.
git clone -o old https://github.com/account/repo.git my-repo
cd my-repo
git remote add new {URL of new repo}
# To pull down the latest updates (assuming you have no local changes):
git checkout {branch(es) of interest}
git pull old
git push --all new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment