Gitignore all except one folder and all its content - regardless of the nesting level

做~自己de王妃 提交于 2020-01-02 07:08:16

问题


I know that this question has been asked many times, but honestly - in spite of many accepted answers, I've not found a neat solution to this problem. I stress the work neat here.

I want to ignore everything except one folder and its entire contents.

This will not achieve it:

*
!dest
!dest/*

that would preserve the dest folder and all files that are in dest, but not other folders within in as well as files in these sub-folders.

Somebody wrote in comments! to one of similar questions that you cannot achieve this without specifying full paths to what you don't want to ignore if you start your .gitignore with *.

I am now convinced that he was right. Or is there a neat way to ignore everything except one folder and its entire content


回答1:


I just tested (with a recent Git, even on Windows)

*
!dest/
!dest/**

As usual, you just have to follow the rule:

It is not possible to re-include a file if a parent directory of that file is excluded. (*)
(*: unless certain conditions are met in git 2.?+, see below)

Once the dest folder is white-listed (!dest/), you can exclude its content (on multiple levels) easily (!dest/**).

From there, any git check-ignore -v dest/x/y/z/aFile would return nothing, which means aFile be added.

I mentioned a similar principle (slightly different solution) in "Gitignore exclude certain files in all subdirectories".


Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

  • commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
  • commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.

Here, this should work with git 2.9+:

*
!dest


来源:https://stackoverflow.com/questions/30792426/gitignore-all-except-one-folder-and-all-its-content-regardless-of-the-nesting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!