Skip to content

Instantly share code, notes, and snippets.

@parollon
Last active August 30, 2023 06:31
Show Gist options
  • Save parollon/d59fc830d78d6ec3b68277abbf333e67 to your computer and use it in GitHub Desktop.
Save parollon/d59fc830d78d6ec3b68277abbf333e67 to your computer and use it in GitHub Desktop.
remove file or directory from all history

Remove file from all Git history!

  1. Use the comamnd

    git filter-branch --force --index-filter \
      'git rm -r --cached --ignore-unmatch [FILE_TO_DELETE]' \
      --prune-empty --tag-name-filter cat -- --all
    
  2. After the filter branch is complete, verify that no unintended file was lost.

  3. Now add a .gitignore rule

    echo [FILE_DELETED] >> .gitignore
    git add .gitignore && git commit -m "ignore rule for [FILE_DELETED]"
    
  4. Now do a push

    echo [FILE_DELETED] >> .gitignore
    git push -f origin branch
    

Check this, this and this for further help. Just to be on the safer side, I would suggest you create a backup copy of the repo on your system before going ahead with these instructions.

References: StackOverflow original answer

Issues

Hey, i'm with the same problem but when typing the command at step 1, i get an error: fatal: bad revision ' --prune-empty'. Any clue?

kevin

Oct 16, 2014 at 21:48

  • @kevin sorry, missed this comment. You can run it without that flag. Pruning would have removed any empty commit object.

    Anshul Goyal

    Feb 2, 2015 at 4:13

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