Remove Unwanted Files From Git History

4.50 avg. rating (87% score) - 2 votes

Here’s an example of how to remove every *.pyc file from every commit in Git history. It is adapted from this Git help page.

Rewrite history

Run git filter-branch, forcing (--force) Git to process—but not check out (--index-filter)—the entire history of every branch and tag (--tag-name-filter cat -- --all), removing the specified files ('git rm --cached --ignore-unmatch *.pyc') and any empty commits generated as a result (--prune-empty).

Be careful! This will overwrite your existing tags.

Add a .gitignore entry

This is to prevent you from committing the files again.

Force-push to remote

If you have a remote repository, the push must be forced so that all remote branches and tags and rewritten.

Fix every copy of the repository

You can delete and re-clone every copy of the repository, or rebase, or delete every affected branch and re-create them.

For example to rebase:

One thought on “Remove Unwanted Files From Git History”

  1. To recursively remove a complete directory tree, use this:

    git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch some/path/i_want_this_dir_gone' --prune-empty --tag-name-filter cat -- --all

Leave a Reply