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

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

# Ignore everything:
*

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

当前回答

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

这对我来说是有效的,我只想向repo提交一个Cordova插件:

...
plugins/*
!plugins/cordova-plugin-app-customization

我有博沃的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

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

关于这一点,有很多类似的问题,所以我将发布我之前写的内容:

我在机器上实现这一点的唯一方法是这样做:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
*.*

#Only Include these specific directories and subdirectories and files if you wish:
!wordpress/somefile.jpg
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*

请注意,您必须明确允许包含每个级别的内容。所以如果我在主题下有5个子目录,我仍然需要把它拼出来。

这是@Yarin在这里的评论:https://stackoverflow.com/a/5250314/1696153

这些是有用的主题:

否定模式如何在.gitignore中工作?gitignore排除规则实际上是如何工作的?

我也试过了

*
*/*
**/**

和**/wp内容/主题/**

或/wp内容/主题/**/*

这一切对我来说都不起作用。很多线索和错误!

与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)