Home > Software engineering >  Rewrite `git` History of a Large File
Rewrite `git` History of a Large File

Time:01-11

I am trying to remove some large files from my git repository.

After a fair amount of browsing I have come across the following solution,

git_delete_file() {
    git filter-branch -f --index-filter "git rm -r --cached --ignore-unmatch $1" --prune-empty
    git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
    git reflog expire --expire=now --all
    git gc --aggressive --prune=now
}

git_file_space() {
    git rev-list --all --objects | 
        git cat-file --batch-check='%(objectname) %(objecttype) %(objectsize) %(rest)' | 
        grep blob |
        sort -k3nr |
        head -n 20
}

However, when I run git_delete_file large/file/path and then git_file_space the files are persisting. I have not tried to download git-filter-repo but I am considering it a last resort.

Regards Jordan

CodePudding user response:

I have not tried to download git-filter-repo but I am considering it a last resort.

It is true you need to install it (after installing Python), but after that, removing a file is simply, using path-based filtering:

git filter-repo --invert-paths --path 'path/to/large/file' 

That seems easier than the all filter-branch update-ref reflog gc workflow.

  •  Tags:  
  • git
  • Related