删除不必要的.keep文件

问题描述

我注意到我的git存储库中有很多.keep文件。当它们的父目录原本为空时,它们曾经很有用,但是从那时起,许多目录现在有了真正的子目录,使它们在git中保持活动状态。

是否有删除所有不需要的.keep文件的好方法?特别是那些:

  1. 大小为0(无实际内容
  2. 准确命名为.keep
  3. 文件夹中有邻居(即删除不会导致其父文件夹变空)

我查看了git gcgit 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...