Git does not have any mention on what a keep file is, so its name is just a convention (.gitkeep
, .keep
, etc). This is why git-clean
is does not support anything like that. I can't think of an extremely easy of doing that, so I ended up with a small script like this:
#!/bin/bash
# read all directories without escaping
while IFS= read -r DIR; do
# enter to each directory to simplify parsing
pushd "$DIR" 1> /dev/null
# ask git to list all tracked files in the current directory
# filtering out the .keep file (the inVerted -v grep switch)
# and checking if it is giving more than 1 line
if [[ $(git ls-files | grep -Pv '^.keep$' | head -1) ]]; then
# if true, just print out the directory along with its "keep" file
echo "$DIR/.keep"
fi
popd 1> /dev/null
# the -mindepth would enable the depth-first traversing
# (empty files only named .keep and never walk into .git directories -- print out directories only)
done < <(find -mindepth 1 -not -path '*/\.git/*' -type f -name '.keep' -empty -printf '%h\n')
This script would print out all redundant .keep
files in dry run. If the generated like looks fine, pipe it with xargs
: above_script_path
| xargs git rm