Skip to content

Instantly share code, notes, and snippets.

@gzagatti
Last active March 14, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gzagatti/35b03946bdb6e4937004d78ab34241e9 to your computer and use it in GitHub Desktop.
Save gzagatti/35b03946bdb6e4937004d78ab34241e9 to your computer and use it in GitHub Desktop.
Removing and Purging Files from Git History

Rewriting history in git

From Removing and purging files from git history

Remove directories added to .gitignore

To remove files which have been recently added to .gitignore.

git ls-files --ignored --exclude-standard | xargs git rm --cached

Deleting entire delete directories

To get a list of entire directories that have been removed from the repository.

git log --all --pretty=format: --name-only --diff-filter=D | sed 's|[^/]+$||g' | sort -u

Rewriting hisotry and remove the old files

Filter the git branch untill all the files of the list have been removed.

git log --all --pretty=format: --name-only --diff-filter=D | sed  's|[^/]+$||g' | sort -u | xargs -I {} git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch "{}"' --prune-empty -f -- --all

Prune all references with garbage collection and reclaim space

rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --aggressive --prune=now

Verify that the files have been removed

Run the same command that was used in the first step above to verify that the files are no longer in history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment