代码之家  ›  专栏  ›  技术社区  ›  PatS

.gitignore忽略带有#的临时文件名的模式是什么

git
  •  0
  • PatS  · 技术社区  · 6 年前

    我有一些临时文件名为:

    #myfile#
    .#anotherfile
    ./some/dir/#myfile#
    ./somewhere/.#anotherfile
    

    我尝试添加(对我来说)明显的模式,参见下面的命令

    cat >> .gitignore <<EOF
    #.*
    .*#.*
    EOF
    

    但是git状态仍然列出了这些文件。

    我意识到散列“#”符号是注释字符,所以我尝试引用它,并添加:

    \#.*
    .*\#.*
    

    但这也不管用。

    进行了一些有益的搜索

    我的.gitignore现在有:

    \#*#
    .\#*#
    

    我现在只有一个需要修正的模式:

    ./somewhere/.#anotherfile
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   torek    6 年前

    正确的地球仪是 *#* 自从 * 匹配零个或多个字符(在名称组件中)。

    当且仅当 # 是第一个角色。既然不是,你就不会( "If it was so, it might be; if it were so, it would be; but as it isn't, it ain't." #ignoreme #thistoo ,但是 忽视 commit#me ,你会想要 #* ,从那以后 #* 开始 具有 # \#*

    (旁白:Git的ignore pattern matcher故意违反 星号与前导点不匹配 行为,即。, *foo .foo 即使是嘘/嘘 不匹配 foo先生 . 也就是说,Git使用 the POSIX fnmatch rules 犹如 FNM_PERIOD

        2
  •  0
  •   PatS    6 年前

    @托雷克,谢谢你的回答。我试过了 *#* 但那没用。

    **.\#*   => handles .#tempfile and sub/dirs/.#tempfile, .#tempfile#, sub/dir/.#tempfile#
    **\#*   => handles #tempfile, sub/dirs/#tempfile, and #tempfile#, sub/dir/#tempfile#
    

    我对模式进行了注释,以显示它匹配的文件,但显然 => text 不会在.gitignore文件中。对我来说这很好,所以这解决了我的问题。

    为了测试,我使用了下面的.gitignore。

    **.\#*
    **\#*
    

    touch '.#file'
    touch '.#file#'
    touch '#file'
    touch '#file#'
    touch 'commit#file'
    touch 'bin/.#file'
    touch 'bin/.#file#'
    touch 'bin/#file'
    touch 'bin/#file#'
    touch 'bin/commit#file'
    

    当我测试这个的时候,所有的文件都被过滤掉了。
    注意:文件

    使用GitBash(在windows上为v2.17.1)和git(在Linux上为1.8.x.x)进行测试。

    我在github上创建了一个小型测试repo来展示这一点。看到了吗 https://github.com/steranka/git-ignore-test