Make.git忽略除少数文件之外的所有内容

Make.git忽略除少数文件之外的所有内容,git,gitignore,Git,Gitignore,我知道.gitignore文件会隐藏Git版本中的指定文件 控制我有一个项目(LaTeX),它会生成很多额外的文件(.auth, .dvi、.pdf、日志等),但我不希望跟踪它们 git ls-files -i --exclude-from .gitignore 我知道我可以(也许应该)让所有这些文件都放在一个文件夹中 项目中单独的子文件夹,因为我可以忽略该文件夹 但是,是否有任何可行的方法将输出文件保存在 项目树并使用.gitignore忽略所有内容,除了我正在创建的文件 用Git跟踪?差不

我知道.gitignore文件会隐藏Git版本中的指定文件 控制我有一个项目(LaTeX),它会生成很多额外的文件(.auth, .dvi、.pdf、日志等),但我不希望跟踪它们

git ls-files -i --exclude-from .gitignore
我知道我可以(也许应该)让所有这些文件都放在一个文件夹中 项目中单独的子文件夹,因为我可以忽略该文件夹

但是,是否有任何可行的方法将输出文件保存在 项目树并使用.gitignore忽略所有内容,除了我正在创建的文件 用Git跟踪?差不多

#忽略一切
*
#但不是这些文件。。。
script.pl
模板乳胶
#等等。。。
可选前缀
它否定模式;被排除的任何匹配文件
以前的模式将再次包括在内。如果否定模式匹配,
这将覆盖优先级较低的模式源

#忽略一切
*
#但不是这些文件。。。
!.gitignore
!script.pl
!模板乳胶
#等等。。。
#…即使它们位于子目录中
!*/
#如果要跟踪的文件位于子目录中
!*/a/b/file1.txt
!*/a/b/c/*

您可以使用git config status.show untracked files no
,所有未跟踪的文件都将对您隐藏。有关详细信息,请参见
mangit config

要忽略目录中的某些文件,必须按照正确的顺序执行此操作:

例如,忽略文件夹“application”中除index.php和文件夹“config”之外的所有内容。注意顺序

你必须先否定你想要的

失败

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

!应用程序/config/*

!application/index.php

有效

webroot/cache*
!webroot/cache/.htaccess
webroot/cache/*
!webroot/cache/.htaccess
!应用程序/config/*

!application/index.php


application/*

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

pippo/*
!皮波/冥王星
皮波/冥王星/*
!pippo/pluto/paperino.xml

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

pippo/*
!pippo/pluto/paperino.xml

它不起作用,因为Git不存在中介
pluto
文件夹,因此
paperino.xml
找不到一个存在的地方。

更具体一点:

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

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

失败

webroot/cache*
!webroot/cache/.htaccess
webroot/cache/*
!webroot/cache/.htaccess
有效

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

我有来自bower的Jquery和Angular。鲍尔把它们安装在

/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

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

你想用
/*
而不是
*
*/
在大多数情况下

使用
*
是有效的,但它是递归工作的。从那时起,它不会查看目录。人们建议使用
*/添加到白名单目录,但实际上最好使用
/*

# Blacklist files/folders in same directory as the .gitignore file
/*

# Whitelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log

# Whitelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

# Adding to the above, this also works...
!/folder/a/deeply
/folder/a/deeply/*
!/folder/a/deeply/nested
/folder/a/deeply/nested/*
!/folder/a/deeply/nested/subfolder
上述代码将忽略所有文件,除了
.gitignore
README.md
文件夹/a/file.txt
文件夹/a/b1/
文件夹/a/b2/
以及最后两个文件夹中包含的所有文件。(这些文件夹中的
.DS_Store
*.log
文件将被忽略。)

显然我能做到,例如
/文件夹
!/。gitignore也可以忽略


更多信息:

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

我让它在我的机器上工作的唯一方法就是这样做:

# 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/*/*/*/*/*
public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
请注意,对于要包含的每个级别,您必须显式地允许内容。所以,如果我在主题下有5个子目录,我仍然需要详细说明

这是@Yarin在这里的评论:

这些都是有用的话题:

我也试过了

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

/wp content/themes/***


这些对我来说都不管用。很多线索和错误

这就是我的工作原理,我只想向repo提交一个Cordova插件:

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

要从.gitignore中排除文件夹,可以执行以下操作

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

这将忽略
bower\u组件中的所有文件/子文件夹
,除了
/highcharts

由于我试图从lib添加一个jar,所以目前为止没有任何效果

这不起作用:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 
这起到了作用:

build/*
!build/libs

如果您需要忽略除少数文件和少数根文件夹以外的所有内容,则简单的解决方案:


神奇之处在于
/*
(如上所述),它忽略(根)文件夹中的所有内容,但不是递归的。

我的子文件夹有问题

不起作用:

/custom/*
!/custom/config/foo.yml.dist
作品:

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

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

git ls-files -i --exclude-from .gitignore
显示了我以这种方式排除的两个文件:

# 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/*/*/*/*/*
public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
最后,这对我很有效:

public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
*
!bin/script.sh
*
!bin
!bin/script.sh
关键是首先否定文件夹
typo3conf/

而且,语句的顺序似乎并不重要。相反,您需要显式地否定所有子文件夹,然后才能否定其中的单个文件

文件夹
!公共/类型3 CONF/<
# Ignore everything
*

# But not these files...
!script.pl
!template.latex
!.gitignore
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
*
!bin/script.sh
*
!bin
!bin/script.sh
.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh
*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
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)
x64/
git add -f x64/Release/myFile.py
git add -f x64/Release/myFile*.py
/data/*
!/data/README.md

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

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