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

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

# Ignore everything:
*

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

当前回答

不确定是否已经指出了这一点,但我在使用!在我希望gitignore忽略的文件夹前面。

然而,我发现被忽略的文件夹路径中没有*。我的gitignore看起来像这样:

/folder/
!/folder/subfolder

子文件夹仍被忽略,但在文件夹后添加一个*使其工作正常

/folder/*
!/folder/subfolder

其他回答

让我们工具化!

正如@Joakim所说,要忽略文件,可以使用下面的方法。

# Ignore everything
*

# But not these files...
!.gitignore
!someFile.txt

但是如果文件位于嵌套目录中,那么编写这些规则有点困难。

例如,如果我们希望跳过所有文件,但不跳过位于aDir/aotherDir/someOtherDir/aDir/bDir/cDir中的.txt文件。那么,我们的.gitignore将是这样的

# Skip all files
*

# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

这很难用手写。

为了解决这个问题,我创建了一个名为git do not ignore的web应用程序,它将为您生成规则。

Tool

https://theapache64.github.io/git-do-not-ignore/

Demo

样本输入

aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

样本输出

!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

我有博沃的Jquery和Angular。Bower将其安装在

/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files

最小化的jquery位于dist目录中,angular位于angular目录中。我只需要最小化的文件就可以提交到github。对gitignore进行了一些篡改,这就是我设法变出来的。。。

/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js

希望有人能发现这个有用。

最简单的方法是强制添加文件。即使它被隐藏或嵌套在git忽略的子目录树中,它也将在git中进行说明。

例如:

x64文件夹已在中排除。gitignore:

x64/

但您希望包含位于x64/发布/目录中的文件myFile.py。然后你必须:

git add -f x64/Release/myFile.py

您可以对匹配模式的多个文件执行此操作,例如。

git add -f x64/Release/myFile*.py

等等

可选前缀!这否定了该模式;排除的任何匹配文件将再次包括先前的模式。如果否定模式匹配,这将覆盖优先级较低的模式源。

# 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/*

如果您想忽略目录中除一个文件外的全部内容,可以为文件路径中的每个目录编写一对规则。例如,.gitignore将忽略pippo文件夹,pippo/pluto/paperino.xml除外

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

请注意,如果您只是在上面写了:

pippo/*
!pippo/pluto/paperino.xml

它无法工作,因为Git中不存在中间的pluto文件夹,因此paperino.xml找不到存在的位置。