miguel.nz

Git – Remove cached files

June 11, 2014   |   1 minute read.

The following bash script can help you removing your cached files on git if you add your file into .gitignore and GIT is already tracking them.

#!/bin/bash
 
cat .gitignore | egrep -v "^#|^$" | while read line; do
  if [ -n "$line" ]; then
    OLD_IFS=$IFS; IFS=""
    for ignored_file in $( git ls-files "$line" ); do
      git rm --cached "$ignored_file"
    done
    IFS=$OLD_IFS
  fi
done

So, create a .sh file on your folder and copy the content above.

vi remove_cached.sh

Save your file and add x permission:

chmod +x remove_cached.sh

Then run your script:

./remove_cached.sh

After run this script you shouldn’t see files you want to ignore under your .gitignore. You can delete the .sh file afterwards.

Source: shell script, How to run a script on mac