Skip to content

Instantly share code, notes, and snippets.

@shiva
Last active March 14, 2024 02:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiva/ed1330c38b06885619ef20b604f8aabb to your computer and use it in GitHub Desktop.
Save shiva/ed1330c38b06885619ef20b604f8aabb to your computer and use it in GitHub Desktop.
Remove files permanently from git

Source: https://stackoverflow.com/questions/2164581/remove-file-from-git-repository-history

This is the shortest way to get rid of the files:

  1. (Optional) Check for the largest files
git verify-pack -v .git/objects/pack/#{pack-name}.idx | sort -k 3 -n | tail -5 
  1. (Optional) Check what are those files
git rev-list --objects --all | grep <id> 
  1. remove a file from all revisions
git filter-branch --index-filter 'git rm --cached --ignore-unmatch <files>'
  1. Remove git backup
rm -rf .git/refs/original/ # to remove git's backup
  1. Expire loose objects
git reflog expire --all --expire='0 days' # to expire all the loose objects
  1. Check for loose objects
git fsck --full --unreachable # to check if there are any loose objects
  1. Repack
git repack -A -d # repacking
  1. Remove and prune
git prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment