首页 文章

如何删除工作副本中所有未版本控制/忽略的文件/文件夹?

提问于
浏览
128

如果我有一个Subversion存储库的工作副本,有没有办法用一个命令或工具删除该工作副本中的所有未版本控制或忽略的文件?基本上,我正在寻找git clean的SVN模拟 .

命令行或GUI解决方案(对于TortoiseSVN)都是可以接受的 .

12 回答

  • 7

    使用TortoiseSVN:

  • 38
    svn status --no-ignore | grep '^[I?]' | cut -c 9- | while IFS= read -r f; do rm -rf "$f"; done
    

    这具有以下功能:

    • 删除已忽略和未跟踪的文件

    • 即使文件名包含空格也是有效的(换行除外,但除了使用 --xml 选项并解析生成的xml输出之外,除此之外没什么可做的)

    • 即使 svn status 在文件名之前打印其他状态字符也是有效的(它不应该因为文件没有被跟踪,但以防万一...)

    • 它应该适用于任何符合POSIX标准的系统

    我使用名为 svnclean 的shell脚本,其中包含以下内容:

    #!/bin/sh
    
    # make sure this script exits with a non-zero return value if the
    # current directory is not in a svn working directory
    svn info >/dev/null || exit 1
    
    svn status --no-ignore | grep '^[I?]' | cut -c 9- |
    # setting IFS to the empty string ensures that any leading or
    # trailing whitespace is not trimmed from the filename
    while IFS= read -r f; do
        # tell the user which file is being deleted.  use printf
        # instead of echo because different implementations of echo do
        # different things if the arguments begin with hyphens or
        # contain backslashes; the behavior of printf is consistent
        printf '%s\n' "Deleting ${f}..."
        # if rm -rf can't delete the file, something is wrong so bail
        rm -rf "${f}" || exit 1
    done
    
  • 7

    我知道这是旧的,但万一其他人偶然发现它,svn的新版本(1.9或更高版本)支持 --remove-unversioned ,例如 svn cleanup . --remove-unversioned .

    https://subversion.apache.org/docs/release-notes/1.9.html#svn-cleanup-options

  • 14

    这个oneliner可能会帮助你:

    $ svn status | grep '^?' | awk '{print $2}' | xargs rm -rf
    

    小心使用!

  • 1

    使用Powershell修改Yanal-Yves Fargialla和gimpf的答案(但不允许对Stackoverflow的原始帖子发表评论):

    powershell -Command "&{(svn status --no-ignore) -match '^[\?i]' -replace '^.\s+' | rm -recurse -force}
    

    这会添加克拉(“^”)以指定行的开头,从而避免匹配包含字母“i”的所有文件 . 还要将-recurse和-force的标志添加到rm,以使此命令非交互式,因此可以在脚本中使用 .

  • 5

    SVN中的许多内容可以通过不同的方式完成,这可以通过这里给出的各种命令行答案来证明 . 随着版本1.7的出现,TortoiseSVN的另一种技术实际上提供了比Stefan提供的答案更精细的粒度分辨率,让您可以分别从被忽略的文件中选择非版本化文件 . 只需选择 TortoiseSvn >> Clean up... 即可打开此对话框 .

    TortoiseSVN cleanup options

  • 4

    使用powershell:

    (svn status --no-ignore) -match '[?]' -replace '^.\s+' | rm
    

    从命令行:

    powershell -Command "&{(svn status --no-ignore) -match '[?]' -replace '^.\s+' | rm}"
    
  • 4

    使用TortoiseSVN:

    • 右键单击工作副本的根目录,然后选择TortoiseSVN - > "check for modifications"

    • 选择"Show ignored files"

    • 按"Text status"列排序

    • 滚动到"non-versioned"文件,现在全部组合在一起;选择它们全部并右键单击 - >删除

    • 滚动到"ignored"文件,现在全部组合在一起;选择它们全部并右键单击 - >删除

    这不是一个很好的和干净的解决方案,但我知道的最快的方式(在Windows上) .

    感谢pkh提示被忽略的文件 .

  • 5

    这个oneliner适合我(基于Richard Hansen的答案,令人惊讶的是它不适用于包含空格的文件):

    svn status --no-ignore | grep '^[I?]' | cut -c 9- | xargs -d"\n" -I{} rm {}
    
  • 95

    这与其他答案类似,但实际上会被忽略的文件(注意RE中的'I'):

    rm -rf `svn status --no-ignore | grep '^[\?I]' | sed 's/^[\?I]//'`
    
  • 127

    有人说你不能从Windows命令行做到这一点 .

    公牛 .

    for /f "tokens=2 delims= " %I IN ('svn st --no-ignore ^| findstr /R "^[I?]"') DO (DEL /S /F /Q /A:H "%I" & rmdir /S /Q "%I")
    

    它是否在一行中并且不需要单个GNU工具 . :)

  • 93

    你不能只用SVN命令行删除它们(不管GUI工具不确定)如果你在Linux系统下这可能会有所帮助:

    http://www.guyrutenberg.com/2008/01/18/delete-unversioned-files-under-svn/

    另一个(野蛮)方法是提交更改,从文件夹中删除所有内容并再次检出 .

相关问题