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

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

# Ignore everything:
*

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

当前回答

与OP有类似的问题,但前10名投票支持的答案中没有一个有效。我终于发现了

错误语法:

*
!bin/script.sh

正确语法:

*
!bin
!bin/script.sh

gitignore手册页的解释:

一个可选前缀“!”,用于否定模式;被先前模式排除的任何匹配文件将再次包含。如果排除了文件的父目录,则无法重新包含该文件。由于性能原因,Git没有列出排除的目录,因此包含的文件上的任何模式都不会产生任何影响,无论它们是在哪里定义的。

这意味着上面的“错误语法”是错误的,因为bin/script.sh不能重新包含,因为bin/被忽略。这就是全部。

扩展示例:

$tree。

.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh

$cat.gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh

$git状态--未跟踪的文件--已忽略

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        bin/sub/folder/path/script.sh

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        bin/ignore.txt
        bin/sub/folder/path/other.sh

nothing added to commit but untracked files present (use "git add" to track)

其他回答

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

例如:

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

x64/

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

git add -f x64/Release/myFile.py

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

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

等等

与OP有类似的问题,但前10名投票支持的答案中没有一个有效。我终于发现了

错误语法:

*
!bin/script.sh

正确语法:

*
!bin
!bin/script.sh

gitignore手册页的解释:

一个可选前缀“!”,用于否定模式;被先前模式排除的任何匹配文件将再次包含。如果排除了文件的父目录,则无法重新包含该文件。由于性能原因,Git没有列出排除的目录,因此包含的文件上的任何模式都不会产生任何影响,无论它们是在哪里定义的。

这意味着上面的“错误语法”是错误的,因为bin/script.sh不能重新包含,因为bin/被忽略。这就是全部。

扩展示例:

$tree。

.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh

$cat.gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh

$git状态--未跟踪的文件--已忽略

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        bin/sub/folder/path/script.sh

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        bin/ignore.txt
        bin/sub/folder/path/other.sh

nothing added to commit but untracked files present (use "git add" to track)

这就是我保持文件夹结构的方式,而忽略其他所有内容。每个目录(或.gitkeep)中都必须有一个README.md文件。

/data/*
!/data/README.md

!/data/input/
/data/input/*
!/data/input/README.md

!/data/output/
/data/output/*
!/data/output/README.md

让我们工具化!

正如@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

我对否定单个文件也有一些问题。我能够提交它们,但我的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的两个不同的东西。

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