How to delete the old git history?

前端 未结 3 1090
你的背包
你的背包 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

    For those who get alot of merge conflicts (and broken results) with rebase --onto I'd like recommend this script which uses git filter-branch:

    #!/bin/sh
    
    cut_sha="$1"
    branch="$2"
    
    git filter-branch \
      --parent-filter "sed -e 's/-p $cut_sha[0-9a-f]*//'" \
      --prune-empty \
      -- $branch
    
    git for-each-ref --format='%(refname)' refs/original | \
      while read ref
      do
        git update-ref -d "$ref"
      done
    
    git reflog expire --expire=0 --all
    git repack -ad
    git prune
    

    Source: https://github.com/adrienthebo/git-tools/blob/master/git-truncate

    Instructions:

    1. Save the script above to local repository root (maybe as git-truncate.sh).
    2. Check out the branch you'd like to truncate (maybe master).
    3. Go down history and find the first (newest) commit SHA you want to cut off (assume it's 2c75a32) AND ensure the commit has no branches in parallel!
    4. Run it like this: $ ./git-truncate.sh 2c75a32 master.
    5. (Push force, if any remote is present.)

    IMPORTANT: The SHA must be "part" of the branch and it must be the first commit you want to delete. Don't pass the first commit you want to keep (the new "beginning of repository" commit)!

提交回复
热议问题