Skip to content

Instantly share code, notes, and snippets.

@masterPiece93
Created December 15, 2025 13:41
Show Gist options
  • Select an option

  • Save masterPiece93/64d63af6d057a99855c0ad112dfe476b to your computer and use it in GitHub Desktop.

Select an option

Save masterPiece93/64d63af6d057a99855c0ad112dfe476b to your computer and use it in GitHub Desktop.

Transferring a GIT repo to new GIT repo with fragmented history

  • suppose you have a GIT repo with a particular point in commit history before which you want to avoid permanently
  • with this approach you'll have a new repo with all history intact post that commit .
  • no links or cache issue what so ever with the old repo .
  1. Identify the hash of the commit you want to be the new starting point (the first commit to keep). Let's call this .
git log --oneline
  1. Create a new, parentless commit (a "root commit") with the exact same content as the commit you want to keep. This command outputs the new commit's hash (let's call it ).
echo "New root commit" | git commit-tree <commit-hash-to-keep>^{tree}
  1. Use git replace to temporarily graft the old commit with the new root commit. This makes Git treat as having no parents.
git replace <commit-hash-to-keep> <new-root-hash>
  1. Run git-filter-repo to make the replacement permanent and rewrite the history of all reachable references.
git filter-repo --force
  1. Clean up the repository by expiring reflogs and aggressively garbage collecting to reclaim space and permanently remove old objects.
git reflog expire --expire=now --all
git gc --prune=now --aggressive
  1. Re-add your remote (e.g., GitHub, GitLab).
# Change the remote origin URL
git remote set-url origin <URL_of_your_new_github_repo>

# Verify the new remote URL
git remote -v

# Force push the filtered history to the new repository
git push --force origin main
  1. Force-push the new history to your remote repository to overwrite the old history.
git push --force --all
git push --force --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment