How to delete the old git history?

前端 未结 3 1087
你的背包
你的背包 2021-01-30 18:10

I have git repository with many, many (2000+) commits, for example:

                 l-- m -- n   
                /
a -- b -- c -- d -- e -- f -- g -- h -- i --         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 18:37

    In order not to lose some history; better first take a copy of your repository :). Here we go: ( is the sha of the commit f that you want to be the new root commit)

    git checkout --orphan temp       # checkout to the status of the git repo at commit f; creating a branch named "temp"
    git commit -m "new root commit"     # create a new commit that is to be the new root commit
    git rebase --onto temp  master   # now rebase the part of history from  to master onthe temp branch
    git branch -D temp                  # we don't need the temp branch anymore
    

    If you have a remote where you want to have the same truncated history; you can use git push -f. Warning this is a dangerous command; don't use this lightly! If you want to be sure that your last version of the code is still the same; you can run git diff origin/master. That should show no changes (since only the history changed; not the content of your files).

    git push -f  
    

    The following 2 commands are optional - they keep your git repo in good shape.

    git prune --progress                 # delete all the objects w/o references
    git gc --aggressive                  # aggressively collect garbage; may take a lot of time on large repos
    

提交回复
热议问题