#!/bin/sh # Remove all files ignored by `git`, except the ones listed in $KEEP below # # `git clean` does not accept "-e" together with "-X", # so the files to be kept are moved aside and then restored # # Usage: git clean-ignored [] set -eu # Keep this list in sync with the other two copies: # - the "-e" flags of `clean-untracked` in ~/.config/git/config # - the preview in ~/.local/bin/git-clean-all KEEP=".env .python-version" SUFFIX=".git-clean-ignored.bak" # `git` runs scripts on the `$PATH` in the current directory, # whereas it runs "!" aliases at the top level of the repository cd "$(git rev-parse --show-toplevel)" _restore() { for _file in $KEEP; do if [ -f "$_file$SUFFIX" ]; then mv "$_file$SUFFIX" "$_file" fi done } # Restore the kept files even if `git clean` fails or the user aborts trap _restore EXIT HUP INT TERM for _file in $KEEP; do if [ -f "$_file" ]; then mv "$_file" "$_file$SUFFIX" fi done git clean -X -d -f "$@"