git - .gitignore 白名单中忽略的子目录

标签 git

<分区>

我正在为我的 Linux 主目录创建一个 git 存储库。除了我列入白名单的文件/目录外,我想忽略大部分内容。我有这个:

# Ignore everything
*
# But not these files...
!.gitignore
!*/scripts/*
# the above line doesn't work, so this is a test:
!./scripts/*

但是它忽略了我添加到 scripts/ 子目录中的文件。我一直在用 -f 添加东西,这是次优的。

这个问题的答案,Git Won't Un-Ignore a Directory ,对我不起作用。

Git 版本 2.1.4。

最佳答案

来自 the docs :

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

您需要显式取消忽略目录 其内容,否则否定内容 的忽略规则无法匹配任何内容。首先,取消忽略 scripts然后您可以取消忽略 scripts/*

这是工作的.gitignore:

*
!.gitignore
!scripts
!scripts/*

就是说,因为您取消了 *,所以您可以只使用一条规则,即取消忽略路径 scripts。第二个 scripts/* 是多余的。

关于git - .gitignore 白名单中忽略的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868155/

相关文章:

git - merge master 中所做的更改,同时保留 git 中本地所做的更改

git - 有什么办法可以通过git log实现Graph by Branch的功能吗?

git - 如何检索 git pull 历史记录

git - 是否有可能在另一个 git repo 中有一个 git repo

git - 我本地的 git repo 有 "not staged"删除的文件,远程是文件,我该如何提取丢失的文件?

javascript - Windows 上的 Node.js Git Bash shebang 失败

GIT merge 和 rebase 工作流程确认?

Git - 比较本地和远程而不先获取?

git - cargo publish 不会忽略 .gitignore 中的文件

git - 如何删除除最后五个之外的所有 Git 提交