I have a file directory like below:
我有一个如下所示的文件目录:
--file1
-lome.txt
--file2
-lome2.txt
--file3
--lome3.txt
I want to ignore all files and subfiles inside the file2
with txt
extension but when I write file2/*.txt
it only ignores lome2.txt
, I know that's because /
in .gitignore
is for ignoring files only in that directory but I have no other choice for separating my folders (since \
doesn't work). so, for accomplishing such a task what should I do?
我想忽略扩展名为txt的File2中的所有文件和子文件,但当我编写File2/*.txt时,它只忽略lome2.txt,我知道这是因为.gitignore中的/只用于忽略该目录中的文件,但我没有其他选择来分隔我的文件夹(因为\不起作用)。那么,为了完成这样的任务,我应该做些什么呢?
更多回答
Try file2/*/*.txt
尝试File2/*/*.txt
it didn't work but file2/**/*.txt worked
不起作用,但File2/**/*.txt起作用了
优秀答案推荐
Put .gitignore
in file2
and in it just ignore *.txt
. Like this:
将.gitignore放入File2中,然后忽略*.txt。就像这样:
echo "*.txt" > file2/.gitignore
file2/.gitignore
works only in file2
. Pattern *.txt
makes Git to ignore all *.txt
files (and directories) recursively.
File2/.gitignore仅适用于File2。模式*.txt使Git递归地忽略所有*.txt文件(和目录)。
更多回答
it 's not a good practice to put .gitignore in a subdirectory, actually I found the solution...you can check the comment section
S把.gitignore放在一个子目录中不是一个好的做法,实际上我找到了解决方案...你可以查看评论区
@alex_121 There is nothing wrong in putting .gitignore
in a subdirectory, people do it frequently when they need it. For example to separate the top-level .gitignore
that ignores build/compile artifacts and a .gitignore
in docs/
that ignores built docs.
@Alex_121将.gitignore放入子目录没有什么错,人们在需要的时候经常这样做。例如,将忽略构建/编译构件的顶级.gitignore和忽略已构建文档的.gitignore分开。
@alex_121 Your file2
seems to be a special subdirectory because you want to ignore *.txt
only in it but not in the entire directory tree. So it only seems natural to put .gitignore
inside the subdirectory and not at the root of the repo.
@Alex_121您的File2似乎是一个特殊的子目录,因为您只想忽略其中的*.txt,而不是整个目录树中的*.txt。因此,将.gitignore放入子目录中而不是放在repo的根目录中似乎是很自然的。
@alex_121 "not a good practice"? Why? What are the downsides?
@Alex_121“不是一个好的做法”?为什么?这样做有什么不利之处?
Some repos have lots of .gitignore
files at different levels. The behaviour is the same, and when you're not sure about what .gitignore
is doing the ignoring of something, you always have git check-ignore -v
有些Repos有很多不同级别的.gitignore文件。行为是相同的,当您不确定.gitignore在做什么忽略某些事情时,您总是使用git check-Ignore-v
我是一名优秀的程序员,十分优秀!