请教一个 gitignore 的写法 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
/div>
iLoveSS
V2EX    git

请教一个 gitignore 的写法

  •  
  •   iLoveSS 2024-08-01 09:51:20 +08:00 3109 次点击
    这是一个创建于 443 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设我的 windows 设备工作目录下有如下文件目录结构
    .gitignore
    .\a
    .\a\1.txt
    .\a\2.dll
    .\a\a1
    .\a\a1\3.txt
    .\a\a1\4.dll

    问题 1:
    如何设置.gitignore 让 git 只追踪 a 目录和 a\a1 目录下的所有 txt

    问题 2:
    如何让 git 只追踪 a 目录及所有递归子目录下的 txt 文件,但排除其它扩展名文件.
    18 条回复    2024-08-01 22:26:25 +08:00
    iLoveSS
        1
    iLoveSS  
    OP
       2024-08-01 09:54:31 +08:00
    问了下 chatgpt3.5, 给的几个答案都不行. 比如

    # Ignore all files in "a" directory except .txt files
    /a/*
    !/a/**/*.txt
    AoEiuV020JP
        2
    AoEiuV020JP  
       2024-08-01 10:09:03 +08:00
    gitignore 是用来“排除”文件的,你不能总想着“只追踪”,应该想你要排除的是什么,
    比如你 b 目录要不要排除掉,按理说你这问法不应该排除 b 目录,但字面上理解就是 a 目录以外的文件全部排除?

    答案有问题就说说是什么文件有问题, 应该排除还是不排除, 结果是排除了还是没排除,提问的艺术,
    iLoveSS
        3
    iLoveSS  
    OP
       2024-08-01 10:12:10 +08:00
    @AoEiuV020JP

    问题 1:
    排除 a 目录下,除了 a/*.txt 和 a/a1/*.txt 以外的文件.

    问题 2:
    排除 a 目录下,除了*.txt 以外的文件.

    这样清晰了吗?
    AoEiuV020JP
        4
    AoEiuV020JP  
       2024-08-01 10:12:25 +08:00
    试了下, 主要坑点应该是子目录不能排除掉,一旦 a1 被排除了,a1 内的文件就无法例外,

    1 ,
    /a/**.*
    !/a/*.txt
    !/a/a1/*.txt

    2,
    /a/**.*
    !/a/**.txt

    你这情况最好应该是多级.gitignore ,a 和 a1 分别不同的规则,
    hxsf
        5
    hxsf  
       2024-08-01 10:13:54 +08:00
    gpt 的答案挺对的
    那些没排除的文件是否在你修改 .gitignore 文件之前就已经被跟踪了?
    iLoveSS
        6
    iLoveSS  
    OP
       2024-08-01 10:17:05 +08:00
    @hxsf 没有被跟踪的, 我新建的测试目录.

    在使用 git init 后,我用 git rm -r --cached . 和 git add . 重新来过也还是没达到目标.
    newaccount
        7
    newaccount  
       2024-08-01 10:19:08 +08:00   1
    问题 1:
    /*
    !/a
    /a/*
    !/a/*.txt
    !/a/a1
    /a/a1/*
    !/a/a1/*.txt

    问题 2:
    /*
    !/a
    /a/**/*.*
    !/a/**/*.txt

    感觉 2 的答案不是很好,对于没有扩展名的可能出问题,还没想到好方法,摸会鱼
    winix
        8
    winix  
       2024-08-01 10:19:34 +08:00   1
    *
    !a/
    !a/**/
    !a/**/*.txt
    AoEiuV020JP
        9
    AoEiuV020JP  
       2024-08-01 10:19:52 +08:00
    @AoEiuV020JP #4 进一步试了下,这里一个重点是,gitignore 无法指定“文件”,但是有办法指定“文件夹”,所以可以加一个规则例外掉文件夹,就可以正常例外子目录里的文件了,
    2 ,
    /a/*
    !/a/**/
    !/a/**.txt
    iLoveSS
        10
    iLoveSS  
    OP
       2024-08-01 10:21:59 +08:00
    @newaccount 挺好的,目前已经能解决我的问题了! 谢谢
    lucasj
        11
    lucasj  
       2024-08-01 10:38:28 +08:00   1
    @newaccount #7 优化一下

    1

    a/*
    !a/*.txt
    !a/a1
    a/a1/*
    !a/a1/*.txt

    2

    a/**/*.*
    !a/**/*.txt
    newaccount
        12
    newaccount  
       2024-08-01 10:45:55 +08:00
    @lucasj 这样子跟 a 同级如果有个 b 目录就有问题了
    iLoveSS
        13
    iLoveSS  
    OP
       2024-08-01 10:50:39 +08:00
    @newaccount 我测试了 11 楼的方法,这个规则对 b 目录下的文件没有影响吧?
    ├───a
    │ ├───a1
    │ └───a2
    └───b
    5qn1H9F2PRhK4rq5
        14
    5qn1H9F2PRhK4rq5  
       2024-08-01 10:53:19 +08:00
    # 忽略所有文件
    *

    # 不忽略根目录所有非目录的文件
    !/*.*


    # 不忽略 a 、a1 所有目录
    !a/
    !a/a1/

    # 不忽略 a\a1 中的 .txt 文件
    !a/*.txt
    !a/a1/*.txt
    newaccount
        15
    newaccount  
       2024-08-01 10:58:11 +08:00
    @iLoveSS 如果 b 目录下有个 a.txt ,那么这个文件也会被追踪,与问题 2 的需求不符
    问题 2 用 8L winix 的写法吧,挺巧妙的解决了
    sbldehanhan
        16
    sbldehanhan  
       2024-08-01 10:59:36 +08:00
    *
    !.gitignore
    !*/
    !*.txt

    只保留 txt 文件和.gitignore 。
    iLoveSS
        17
    iLoveSS  
    OP
       2024-08-01 11:26:48 +08:00
    @newaccount 谢谢大佬, 这里可能有点理解的偏差. 我的本意是只针对工作目录下的 a 目录内容进行排除. 所以 b 目录不在筛选之内也是对的.
    oeyoews
        18
    oeyoews  
       2024-08-01 22:26:25 +08:00 via iPhone
    gitignore 文件可以有多个
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1457 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 51ms UTC 16:48 PVG 00:48 LAX 09:48 JFK 12:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86