我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
当前回答
如果您想忽略目录中除一个文件外的全部内容,可以为文件路径中的每个目录编写一对规则。例如,.gitignore将忽略pippo文件夹,pippo/pluto/paperino.xml除外
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
请注意,如果您只是在上面写了:
pippo/*
!pippo/pluto/paperino.xml
它无法工作,因为Git中不存在中间的pluto文件夹,因此paperino.xml找不到存在的位置。
其他回答
可选前缀!这否定了该模式;排除的任何匹配文件将再次包括先前的模式。如果否定模式匹配,这将覆盖优先级较低的模式源。
# 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/*
要忽略目录中的某些文件,必须按正确的顺序执行此操作:
例如,忽略文件夹“application”中除index.php和文件夹“config”外的所有内容,注意顺序。
你必须先否定你想要的。
失败
应用程序/*
!应用程序/配置/*
!应用程序/index.php
作品
!应用程序/配置/*
!应用程序/index.php
应用程序/*
关于这一点,有很多类似的问题,所以我将发布我之前写的内容:
我在机器上实现这一点的唯一方法是这样做:
# 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内容/主题/**/*
这一切对我来说都不起作用。很多线索和错误!
在大多数情况下,您希望使用/*而不是*或*/
使用*是有效的,但它以递归方式工作。从那时起,它就不会查看目录了。人们建议使用!*/再次包含列表目录,但实际上最好用/*
# Blocklist files/folders in same directory as the .gitignore file
/*
# Includelist some files
!.gitignore
!README.md
# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log
# Includelist 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也是。
更多信息:http://git-scm.com/docs/gitignore
更具体一点:
示例:忽略webroot/cache中的所有内容,但保留webroot/cache/.htaccess。
请注意缓存文件夹后面的斜杠(/):
失败
webroot/cache*
!webroot/cache/.htaccess
作品
webroot/cache/*
!webroot/cache/.htaccess