Gitignore everything except a subfolder (with every content)?

后端 未结 3 536
感动是毒
感动是毒 2020-12-03 20:06

I want to ignore everything, except a specific subfolder (and all of its contents!). I tried solutions from possible duplicate questions without any success.

I\'d ne

相关标签:
3条回答
  • 2020-12-03 20:48
    *
    !*/
    !That/Very/Folder/**
    !Also/This/Another/Folder/**
    

    Ignore everything, allow subfolders (!), then allow specific folder contents (with unlimited subfolders within).

    Credits to @Jepessen for the middle piece that makes it work.

    0 讨论(0)
  • 2020-12-03 20:58

    I want to ignore everything

    add the folder to the gitignore

    except a specific subfolder (and all of its contents!).

    force folder to be added to the repository

    git add -f folder
    

    EDIT:

    I use this solution for example when I need to keep log folder, for example, but not its content. Generally, when I suppose the content of the folder is never to be added. And generally I add just path/to/folder/.gitkeep file with -f option.

    0 讨论(0)
  • 2020-12-03 21:05

    Your .gitignore almost works but it doesn't for a simple reason: the first rule (*) tells Git to ignore every file and directory in the root of the repository. Git honors it and ignores everything, including the That directory and its content. The "unignore" rules that follow do not match anything inside the That subdirectory because the That directory is ignored together with it content, and they don't have effect.

    In order to tell Git to not ignore files and directories in a deeply nested sub-directory you have to write ignore and unignore rules to let it reach the enclosing sub-directory first and then add the rules you want.

    Your .gitignore file should look like this:

    ### Ignore everything ###
    *
    
    # But do not ignore "That" because we need something from its internals...
    !That/
    
    # ... but ignore (almost all) the content of "That"...
    That/*
    # ... however, do not ignore "That/Very" because we need to dig more into it
    !That/Very/
    
    # ... but we don't care about most of the content of "That/Very"
    That/Very/*
    # ... except for "That/Very/Folder" we care
    !That/Very/Folder/
    # ... and its content
    !That/Very/Folder/*
    
    0 讨论(0)
提交回复
热议问题