我知道.gitignore文件隐藏Git版本控制中的指定文件。

我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

当前回答

我的子文件夹有问题。

不工作:

/custom/*
!/custom/config/foo.yml.dist

作品:

/custom/config/*
!/custom/config/foo.yml.dist

其他回答

更具体一点:

示例:忽略webroot/cache中的所有内容,但保留webroot/cache/.htaccess。

请注意缓存文件夹后面的斜杠(/):

失败

webroot/cache*
!webroot/cache/.htaccess

作品

webroot/cache/*
!webroot/cache/.htaccess

我对否定单个文件也有一些问题。我能够提交它们,但我的IDE(IntelliJ)总是抱怨被忽略的文件被跟踪。

git ls-files -i --exclude-from .gitignore

显示了两个文件,我以这种方式排除了这两个文件:

public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

最后,这对我有用:

public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

关键是对文件夹typeo3conf/first的否定。

此外,声明的顺序似乎无关紧要。相反,您需要显式否定所有子文件夹,然后才能否定其中的单个文件。

文件夹!public/type3conf/和文件夹内容public/pype3conf/*是.gitignore的两个不同的东西。

伟大的线程!这个问题困扰了我一段时间;)

# ignore these
*
# except foo
!foo

到目前为止,没有什么对我有用,因为我试图从lib中添加一个jar。

这不起作用:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 

这是有效的:

build/*
!build/libs

我是这样做的:

# Ignore everything
*

# Whitelist anything that's a directory
!*/

# Whitelist some files
!.gitignore

# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**

# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules

# Ignore this file recursively
**/.DS_Store

使用gig status-u递归查看未跟踪目录中的单个文件-使用git status,您只能看到文件夹,这可能会让您误以为其中的所有内容都被跟踪