Gitignore 排除子文件夹(带有 **/pattern)除了特定的

标签 git

我在 SO 上搜索这个答案,但没有任何相似之处,或者我迷路了。我正在尝试排除特定子文件夹中除一个文件夹之外的所有内容。可能我可以手动完成所有路径,但我认为应该有聪明的方法。我的文件结构:

.gitignore
test/
    file1
    file2
    sim_case/
        file1
        file2
            include/
                file1
                file2

在 .gitignore 中我有:

**/sim_case

这排除了 sim_case 中的所有内容,但我想包含文件夹“include”。我尝试了一些变体,但没有任何效果。

!**/sim_case/include
!**/sim_case/include/
!**/sim_case/include/*
!sim_case/include/
!sim_case/include/
!sim_case/include/*

最佳答案

我几乎没有做任何研究,看来仅排除“sim_case”文件夹中的文件的最佳选择是使用:

**/sim_case/*
!**/sim_case/include

这允许有更复杂的文件结构

test/sim_case/include
test1/sim_case/include
etc.

“git status”的结果:

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .gitignore
    new file:   test/file1
    new file:   test/file2
    new file:   test/sim_case/include/file1
    new file:   test/sim_case/include/file2
    new file:   test1/file1
    new file:   test1/file2
    new file:   test1/sim_case/include/file1
    new file:   test1/sim_case/include/file2

关于Gitignore 排除子文件夹(带有 **/pattern)除了特定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695042/

相关文章:

git - 克隆没有标签的仓库?

git pull 自动 merge

git subtree merge on the root of repository (with.git-flow)

git - 如何查看裸仓库中的文件?

git - 在服务器端复制 git 客户端 Hook

git - 简单的 Git 分支切换工作流程

linux - 如何将 "shadow"git 存储库与主存储库 "secretly"同步

svn - 如何通过 git-svn 使用嵌套分支

MacOS终端: how to use a seccond ssh key?

git - 为什么 git submodule update 失败并显示 "fatal: remote error: upload-pack: not our ref"?