gitgitignore

Delete any file or folder with a pattern from git cache and previous commits


I want to remove any files or directories that match the following patterns:

**/.* 
**/__*

I tried the following commands:

find . -path './.git' -prune -o -name '.*' ! -name '.' -exec git rm --cached {} +
find . -path './.git' -prune -o -name '__*' ! -name '.' -exec git rm --cached {} +

For example, the specific patterns I want to remove include:

**/.venv/
**/.vscode/
**/.idea/ 
**__pycache__/
**__temp__/
**.trash_bin/
**.gitignore
**.git.sh
**/.git/
**/.env/    

What is the command to delete all previous commits in the repository?


Solution

  • Make a backup. Do a fresh clone of your repository. Put the list of patterns to remove into a file, say "patterns", and prepend every line with glob: so the file should looks like this:

    glob:**/.venv/
    glob:**__pycache__/
    glob:**__temp__/
    

    cd into the fresh clone and run the command

    git filter-repo --invert-paths --paths-from-file ../patterns
    

    Path to the file could be relative, git filter-repo doesn't change its current directory.