.gitignore,排除文件夹中的所有文件..但保留那些带有.gitkeep的子文件夹?



我想忽略文件夹中的所有文件log除了.gitkeep文件(我需要这些文件来保留空目录(:

- log
|- foo.log (should be ignored)
|- folder1 (should be ignored)
|- folder2 (keep it because contains a .gitkeep file)
|- .gitkeep

不工作:

/log/*
!/log/*/.gitkeep

使用**也不起作用:

/log/*
!/log/**/.gitkeep

这可能吗...无需手动排除每个子文件夹,就像这样?

/log/folder1/*
/log/folder2/*
!/log/folder2/.gitkeep

*忽略的取消忽略目录:

/log/**
!/log/*/
!/log/*/.gitkeep

没有这个git甚至不会查看被忽略的子目录。

@phd 的答案的变体,适用于任何级别的子文件夹,而不仅仅是第一级子目录:

# Ignore all files within a directory (recursively)
/log/**
# But do not ignore any directory or subdirectory it contains
!/log/**/
# Do not ignore a special file name
!.gitkeep

这在以下情况下进行了测试:

.
└── log/
├── .gitkeep
├── dirA/
│   ├── dirAA/
│   │   ├── .gitkeep
│   │   └── fileAAA.txt
│   └── fileAA.txt
├── dirB/
│   ├── .gitkeep
│   └── fileBA.txt
└── file.txt

现在,git add log/产生:

user@machine$ git add log/
user@machine$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file:   log/.gitkeep
new file:   log/dirA/dirAA/.gitkeep
new file:   log/dirB/.gitkeep
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
.gitignore.swp

注意:虽然git add只能添加文件(而不是空文件夹(,但取消忽略文件夹(使用!/log/**/(很重要,否则 git 甚至不会搜索这些文件夹中的.gitkeep文件。

(另外,请记住,**的含义只是"可能包含包含"/"的任何字符的文件路径/名称,而*与不包括"/"的任何字符匹配。

最新更新