How do I reduce the size of a bloated Git repo by non-interactively squashing all commits except for the most recent ones?

前端 未结 3 1284
萌比男神i
萌比男神i 2020-12-19 04:03

My Git repo has hundreds of gigabytes of data, say, database backups, so I\'m trying to remove old, outdated backups, because they\'re making everything larger and slower. S

3条回答
  •  旧巷少年郎
    2020-12-19 04:50

    The original poster comments:

    if we take a snapshot of a commit 10004, remove all commits before it, and make commit 10004 a root commit, I'll be just fine

    One way to do this is here, assuming your current work is called branchname. I like to use a temp tag whenever I do a large rebase to double-check that there were no changes and to mark a point I can reset back to if something goes wrong (not sure if this is standard procedure or not but it works for me):

    git tag temp
    
    git checkout 10004
    git checkout --orphan new_root
    git commit -m "set new root 10004"
    
    git rebase --onto new_root 10004 branchname
    
    git diff temp   # verification that it worked with no changes
    git tag -d temp
    git branch -D new_root
    

    To get rid of the old branch you'll need to delete all tags and branch tags on it; then

    git prune
    git gc
    

    will clean it from your repo.

    Note that you'll temporarily have two copies of everything, until you have gc'd, but that is unavoidable; even if you do a standard squash and rebase you still have two copies of everything until the rebase finishes.

提交回复
热议问题