我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
当前回答
这里有很多复杂的答案。。。这里有一些我在上面看不到的非常简单的东西,在许多场景中都很有效:
# ignore all files that have an extension
**/*.*
# except for these extensions
!**/*.extension1
!**/*.extension2
这不会忽略任何无扩展名的文件,但是如果您有一堆文件都具有相同或相似的名称,则可以为这些文件添加排除项
**/EXTENSIONLESSFILETOIGNORE
其他回答
如果您想忽略目录中除一个文件外的全部内容,可以为文件路径中的每个目录编写一对规则。例如,.gitignore将忽略pippo文件夹,pippo/pluto/paperino.xml除外
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
请注意,如果您只是在上面写了:
pippo/*
!pippo/pluto/paperino.xml
它无法工作,因为Git中不存在中间的pluto文件夹,因此paperino.xml找不到存在的位置。
# ignore these
*
# except foo
!foo
我似乎找到了其他人没有提到的对我有用的东西。
# Ignore everything
*
# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...
# And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
!subdir/
!subdir/**/*
基本上,它似乎否定了被忽略的子目录,您必须有两个条目,一个用于子目录本身!subdir/和另一个扩展到其下的所有文件和文件夹!副驾驶室/**/*
到目前为止,没有什么对我有用,因为我试图从lib中添加一个jar。
这不起作用:
build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar
这是有效的:
build/*
!build/libs
如果您需要忽略除少数文件和少数根文件夹之外的所有内容,则简单的解决方案:
/*
!.gitignore
!showMe.txt
!my_visible_dir
神奇的是在/*(如上所述)中,它忽略(根)文件夹中的所有内容,但不递归。