我知道.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
当前回答
您可以使用git-config-status.showUntrackedFiles no,所有未跟踪的文件都将对您隐藏。有关详细信息,请参阅man git config。
其他回答
# ignore these
*
# except foo
!foo
我尝试了上面给出的所有答案,但没有一个对我有效。在阅读gitignore文档(此处)后,我发现如果首先排除文件夹,则子文件夹中的文件名不会被索引。因此,如果您随后使用感叹号来包含文件,则在索引中找不到该文件,因此不会包含在git客户端中。
这就是找到解决方案的方法。我开始为我的文件夹树中的所有子文件夹添加例外以使其正常工作,这是一项艰巨的工作。之后,我能够将详细配置压缩到下面的配置,这有点与文档相反。。
正在工作.gitignore:
# Ignore the 'Pro' folder, except for the '3rdparty' subfolder
/Pro/*
!Pro/3rdparty/
# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/
# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
结果,我在我的git客户端中看到,只有Pro/3rdparty/domain/modulename/文件夹中的两个文件正在准备下一次提交,而这正是我想要的。
如果您需要将同一文件夹的多个子文件夹列入白名单,请将exclude语句下面的感叹号行分组,如下所示:
# Ignore the 'Pro' folder, except for the '3rdparty' subfolder
/Pro/*
!Pro/3rdparty/
# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/
# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/
否则它将无法按预期工作。
可选前缀!这否定了该模式;排除的任何匹配文件将再次包括先前的模式。如果否定模式匹配,这将覆盖优先级较低的模式源。
# Ignore everything
*
# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...
# ...even if they are in subdirectories
!*/
# if the files to be tracked are in subdirectories
!*/a/b/file1.txt
!*/a/b/c/*
到目前为止,没有什么对我有用,因为我试图从lib中添加一个jar。
这不起作用:
build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar
这是有效的:
build/*
!build/libs
我似乎找到了其他人没有提到的对我有用的东西。
# 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/和另一个扩展到其下的所有文件和文件夹!副驾驶室/**/*