Git忽略和取消忽略不起作用?

16

我看过了这个答案:Unignore subdirectories of ignored directories in Git

据我所知,我正在做同样的事情,但是 git 拒绝取消忽略我的目录/文件。

这些是我的规则。我试图只获取列出的目录/子目录,但整个 public 目录都没有显示出来。

/public/bootstrap/*
!/public/bootstrap/bower_components/bootstrap/dist/*
!/public/bootstrap/bower_components/bootstrap/dist/**/*
!/public/bootstrap/bower_components/bootstrap/less/variables.less

有什么想法吗?


我刚刚复制了这个结构,但 variables.less 没有被忽略。你确定没有其他的 .gitignore 文件会捕捉到它吗? - John Feminella
在整个项目文件夹中进行了查找,唯一的其他.gitignore位于不同的目录中,只包含“local”。 - Joren
3个回答

11
您可以按如下方式操作。句子"You can't unignore more than one level deep per line"不正确。您可以参考这里的答案。
/public/bootstrap/**
!/public/bootstrap/**/
!/public/bootstrap/bower_components/bootstrap/dist/**
!/public/bootstrap/bower_components/bootstrap/less/variables.less

所以如果我理解正确的话,第一行忽略所有内容,第二行取消忽略所有目录,第三行取消忽略dist中的所有文件和目录?而且,如果我理解正确的话,现在所有文件夹都已经取消忽略了,但由于非dist文件夹中的文件被忽略了,git不会对它们做任何操作? - Joren

7

在进行了一些阅读和大量的试错后,以下方法可行:

/public/bootstrap/bower_components/jquery
/public/bootstrap/bower_components/bootstrap/**
!/public/bootstrap/bower_components/bootstrap/dist/
!/public/bootstrap/bower_components/bootstrap/dist/**
!/public/bootstrap/bower_components/bootstrap/less/
!/public/bootstrap/bower_components/bootstrap/less/variables.less

问题似乎在文档中的这一行:“如果排除了文件的父目录,则无法重新包含该文件。”。
这似乎直接与其他答案相矛盾,但对我来说似乎是这样工作的。
在我的测试中,我发现如果您像这样忽略它:
/public/bootstrap/*

每行只能取消忽略一层。

# doesn't work
!/public/bootstrap/bower_components/bootstrap/

# works
!/public/bootstrap/bower_components/
!/public/bootstrap/bower_components/bootstrap/

3
以下是一条帮助你测试是否正确的命令行:https://git-scm.com/docs/git-check-ignore 以下是相关的 CLI 命令:
git check-ignore -v .idea/bbb.txt .idea/workspace.xml .idea/runConfigurations/Android_Debug___ReactNative.xml android/.idea/aaaa.txt android/.idea/runConfigurations/app.xml

输出以下内容:
.gitignore:33:**/.idea/**   .idea/bbb.txt
.gitignore:33:**/.idea/**   .idea/workspace.xml
.gitignore:35:!**/.idea/runConfigurations/* .idea/runConfigurations/Android_Debug___ReactNative.xml
.gitignore:33:**/.idea/**   android/.idea/aaaa.txt
.gitignore:35:!**/.idea/runConfigurations/* android/.idea/runConfigurations/app.xml

请注意第3行和第5行已成功取消忽略(请注意"!"标志)。
我的.gitignore文件:
**/.idea/**
!**/.idea/runConfigurations/
!**/.idea/runConfigurations/*

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接