【问题标题】:.gitignore track only one file in a directory at any depth.gitignore 只跟踪目录中任意深度的一个文件
【发布时间】:2020-06-09 15:47:31
【问题描述】:

我试图忽略命名目录中除单个文件之外的所有文件,该文件可能位于任何深度。所以在这样的结构中,我想跟踪 build/.gitkeep 的任何出现,但忽略 build/ 中和下面的所有其他文件。要跟踪的项目标有 ++。要忽略的项目标有--。

    |.gitignore                               ++
    |Makefile                                 ++
    |--build/                                 ++
    |--build/.gitkeep                         ++
    |--build/ignore_any_file                  --
    |--build/ignore_any/dir                   --
    |--foo/                                   ++
    |--foo/file_to_keep                       ++
    |--foo/dir_to_keep/                       ++
    |--foo/dir_to_keep/stuff                  ++
    |--foo/build/                             ++
    |--foo/build/.gitkeep                     ++
    |--foo/build/ignore_any_file              --
    |--foo/build/ignore_any/dir               --
    |--foo/build/ignore_any/dir/subir         --
    |--bar                                    ++
    |--bar/file_to_keep                       ++
    |--bar/dir_to_keep/                       ++
    |--bar/dir_to_keep/stuff                  ++
    |--bar/build/                             ++
    |--bar/build/.gitkeep                     ++
    |--bar/ignore_any_file                    --
    |--bar/ignore_any/dir                     --
    |--bar/ignore_any/dir/subir               --
    |--foobar/foo                             ++
    |--foobar/foo/file_to_keep                ++
    |--foobar/foo/dir_to_keep/                ++
    |--foobar/foo/dir_to_keep/stuff           ++
    |--foobar/foo/build/                      ++
    |--foobar/foo/build/.gitkeep              ++
    |--foobar/foo/build/ignore_any_file       --
    |--foobar/foo/build/ignore_any/dir        --
    |--foobar/foo/build/ignore_any/dir/subir  --

我尝试了很多很多组合。对我来说,症结似乎是在任何深度都有构建/。我试图避免必须为 build/ 存在的每个路径创建条目。我也不想在 build/ 的每个实例中创建 .gitignore 文件。

    $ git --version
    git version 2.20.1

【问题讨论】:

    标签: git gitignore


    【解决方案1】:

    如果您希望能够排除文件,则应确保从忽略规则中排除 文件夹

    build/**
    !build/**/
    !build/**/.gitkeep
    

    这是因为:

    It is not possible to re-include a file if a parent directory of that file is excluded

    在你的情况下(构建任何深度)

    **
    !**/
    !build/.gitkeep
    !**/build/.gitkeep
    

    OP nelsonov 使其工作 (in the comments) 与:

    build/** 
    !build/**/ 
    !build/ 
    !build/.gitkeep 
    !**/build/ 
    !**/build/.gitkeep 
    **/build/**
    

    【讨论】:

    • 在你的版本中,我仍然得到 build/file 和 build/dir/(但不是 build/dir/file)。看起来我仍然得到了我不应该得到的一层。我很熟悉排除父项后无法重新包含某些内容的概念,但我似乎排除的不够。
    • @nelsonov 好的,我编辑后的答案中的第二个提案呢?
    • 结果对我来说似乎相同。以下是一些显示为未跟踪的文件示例(应忽略):GatewayESP8266-nrf24/build/file GatewayESP8266-nrf24/build/somedir/ GatewayESP8266-rfm95/build/ mysensors-eeprom/build/EEProm.ino.eep mysensors-eeprom/build/build.options.json mysensors-eeprom/build/core/
    • @nelsonov 你的 .gtiignore 在哪里?我刚刚测试了它,它工作得很好。未检测到较低的build 文件夹,即使它们包含.gitkeep。你使用的是什么版本的 Git?在哪个操作系统上?
    • Linux 上的 git 版本 2.20.1(Rasbian 10 和 Debian 10).gitignore 位于 git 存储库的根目录中为了澄清(可能是必要的)我想在任何深度跟踪 build/.gitkeep 的每个实例但在任何深度都忽略build/ 中的任何其他内容。
    猜你喜欢
    • 2017-03-20
    • 2021-02-24
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多