Skip to content

Instantly share code, notes, and snippets.

@knu
Created July 8, 2019 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knu/5f5dc489dab39ca1220adff181f56fc5 to your computer and use it in GitHub Desktop.
Save knu/5f5dc489dab39ca1220adff181f56fc5 to your computer and use it in GitHub Desktop.
Interactive git clean using a selector tool like peco or fzf
#!/bin/sh
set -e
while getopts 'x' opt; do
case "$opt" in
x)
flags=--ignored
;;
*)
exit 64
;;
esac
done
shift $((OPTIND - 1))
lines="$(git status --porcelain $flags | sed -n '/^[?!][?!]/p')"
if [ -z "$lines" ]; then
echo "no files to clean"
exit 0
fi
selector=$(git config selector.multiple)
: ${selector:=peco}
lines="$(printf %s "$lines" | $selector)"
[ -z "$lines" ] && exit 0
perl -e 'print "$1\0" while $ARGV[0] =~ /^.. (.*)$/mg' -- "$lines" | (
cd "$(git rev-parse --show-toplevel)"
xargs -0 rm -v
)
@knu
Copy link
Author

knu commented Jul 8, 2019

To use fzf instead of peco, run this:

git config --global selector.single fzf
git config --global selector.multiple "fzf -m"

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