Created
July 8, 2019 10:43
-
-
Save knu/5f5dc489dab39ca1220adff181f56fc5 to your computer and use it in GitHub Desktop.
Interactive git clean using a selector tool like peco or fzf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use fzf instead of peco, run this: