问题描述
我注意到我的git存储库中有很多.keep
文件。当它们的父目录原本为空时,它们曾经很有用,但是从那时起,许多目录现在有了真正的子目录,使它们在git中保持活动状态。
我查看了git gc
,git clean
等文档,但是没有找到这样的功能。
解决方法
您可以从Random 'concerns' folders and '.keep' files中读到.keep
文件,这些文件只是有用的文件,可以将文件夹“提交”到存储库中。
这是删除所有.keep
文件的命令。然后只需根据需要提交即可。
zrrbite@ZRRBITE MINGW64 /d/dev/git/keeptest (master)
$ git ls-files *.keep | xargs git rm
rm '.keep'
rm 'test/.keep'
zrrbite@ZRRBITE MINGW64 /d/dev/git/keeptest (master)
$ git st
## master
D .keep
D test/.keep
,
Git并没有提及 keep 文件是什么,因此它的名称只是一个约定(.gitkeep
,.keep
等)。这就是git-clean
是不支持这样的原因的原因。我觉得这样做非常简单,所以我最终得到了一个像这样的小脚本:
#!/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')
此脚本将以空运行方式打印出所有冗余.keep
文件。如果生成的图片看起来不错,请用xargs
用管道传输: above_script_path
| xargs git rm