June 15, 2021

Git tricks

Remove files from git without deleting

Remove one file from git without deleting local copy

git rm --cached mylogfile.log

To remove thousands of files in directory from git only:

find mydirectory/ -type f | xargs git rm --cached

Completely remove one or more files from git history

First, change directory to root of repository...

Here is how to completely remove all .tar.bz2 files from the repository (all commits):

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.tar.bz2' --prune-empty --tag-name-filter cat -- --all

Remove specific file (remember, must give full path to file):

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch GIT/PATH/TO/FILE.EXT' --prune-empty --tag-name-filter cat -- --all

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.