我知道.gitignore文件隐藏了Git版本控制中指定的文件.我有一个项目(LaTeX),它在运行时会生成许多额外的文件(.auth,.dvi,.pdf,日志等),但我不希望这些文件被跟踪.
我知道我可以(也许应该)这样做所有这些文件放在项目中的一个单独的子文件夹中,因为我可以忽略该文件夹.
但是,有没有可行的方法将输出文件保存在项目树的根目录中,并使用.gitignore忽略除了我用Git跟踪的文件之外的所有内容?就像是
# Ignore everything
*
# But not these files...
script.pl
template.latex
# etc...
Run Code Online (Sandbox Code Playgroud)
Joa*_*son 2204
一个
!否定模式的可选前缀; 之前模式排除的任何匹配文件将再次包含在内.如果否定模式匹配,则将覆盖较低优先级模式源.
# 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/*
Run Code Online (Sandbox Code Playgroud)
Giu*_*ano 588
如果要忽略除了其中一个文件的目录的整个内容,可以为文件路径中的每个目录编写一对规则.例如.gitignore忽略pippo文件夹,除了pippo/pluto/paperino.xml
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
Run Code Online (Sandbox Code Playgroud)
Rya*_*lor 220
您想要使用/*而不是*或*/在大多数情况下使用
使用*是有效的,但它以递归方式工作.从那时起它不会查看目录.人们建议!*/再次使用白名单目录,但实际上最好将最高级别的文件夹列入黑名单/*
# 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
Run Code Online (Sandbox Code Playgroud)
上面的代码会忽略所有的文件,除了.gitignore,README.md,folder/a/file.txt,folder/a/b1/和folder/a/b2/和包含在那些过去的两个文件夹的一切.(和.DS_Store和*.log文件将这些文件夹中被忽略.)
显然我可以做!/folder或不做!/.gitignore.
更多信息:http://git-scm.com/docs/gitignore
Nik*_*Nik 69
更具体一点:
示例:忽略所有内容webroot/cache- 但请保留webroot/cache/.htaccess.
注意cache文件夹后面的斜杠(/):
失败
webroot/cache*
!webroot/cache/.htaccess
Run Code Online (Sandbox Code Playgroud)
作品
webroot/cache/*
!webroot/cache/.htaccess
Run Code Online (Sandbox Code Playgroud)
sla*_*nje 27
要忽略目录中的某些文件,您必须按正确的顺序执行此操作:
例如,忽略文件夹"application"中的所有内容,除了index.php和文件夹"config" 注意顺序.
你必须首先否定你想要的东西.
失败
application/*
!application/config/*
!application/index.php
作品
!application/config/*
!application/index.php
application/*
frn*_*ntn 24
有与 OP 类似的问题,但前 10 名赞成的答案中没有一个实际有效。
我终于发现了以下内容
错误的语法:
*
!bin/script.sh
Run Code Online (Sandbox Code Playgroud)
正确的语法:
*
!bin
!bin/script.sh
Run Code Online (Sandbox Code Playgroud)
来自gitignore 手册页的解释:
一个可选的前缀“!” 这否定了模式;任何被先前模式排除的匹配文件将再次包含在内。如果排除了该文件的父目录,则无法重新包含该文件。出于性能原因,Git 不会列出排除的目录,因此包含文件的任何模式都不起作用,无论它们在哪里定义。
这意味着上面的“错误语法”是错误的,因为bin/script.sh不能像bin/被忽略一样重新包含。就这样。
扩展示例:
$树。
.
??? .gitignore
??? bin
??? ignore.txt
??? sub
??? folder
??? path
??? other.sh
??? script.sh
Run Code Online (Sandbox Code Playgroud)
$猫.gitignore
*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
Run Code Online (Sandbox Code Playgroud)
$ git status --untracked-files --ignored
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)
Run Code Online (Sandbox Code Playgroud)
小智 15
要从.gitignore中排除文件夹,可以执行以下操作.
!app/
app/*
!app/bower_components/
app/bower_components/*
!app/bower_components/highcharts/
Run Code Online (Sandbox Code Playgroud)
这将忽略bower_components除里面的所有文件/子文件夹/highcharts.
Kat*_*tie 13
关于这一点有很多类似的问题,所以我会发布我之前写过的内容:
我在机器上工作的唯一方法是这样做:
# 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/*/*/*/*/*
Run Code Online (Sandbox Code Playgroud)
请注意您必须明确允许要包含的每个级别的内容.因此,如果我在主题下有5个子目录,我仍然需要拼写出来.
这是来自@ Yarin的评论:https://stackoverflow.com/a/5250314/1696153
这些是有用的主题:
我也试过了
*
*/*
**/**
Run Code Online (Sandbox Code Playgroud)
和 **/wp-content/themes/**
要么 /wp-content/themes/**/*
这些也不适用于我.很多线索和错误!
Dan*_*sen 12
不确定是否已经指出了这一点,但我在使用我!希望 gitignore 忽略的文件夹前面的 忽略被忽略文件夹内的子文件夹时遇到了麻烦。
但是我发现被忽略的文件夹的*路径中没有。我的 .gitignore 看起来像这样:
/folder/
!/folder/subfolder
Run Code Online (Sandbox Code Playgroud)
子文件夹仍然被忽略,但添加*后文件夹使其工作起来像这样
/folder/*
!/folder/subfolder
Run Code Online (Sandbox Code Playgroud)
Rei*_*ken 11
这里有很多复杂的答案......这是我在上面没有看到的非常简单的东西,并且在许多情况下都运行良好:
# ignore all files that have an extension
**/*.*
# except for these extensions
!**/*.extension1
!**/*.extension2
Run Code Online (Sandbox Code Playgroud)
这不会忽略任何无扩展名文件,但如果您有一堆具有相同或相似名称的文件,您可以为这些文件添加排除项
**/EXTENSIONLESSFILETOIGNORE
Run Code Online (Sandbox Code Playgroud)
Fab*_*one 10
我有一个子文件夹的问题.
不起作用:
/custom/*
!/custom/config/foo.yml.dist
Run Code Online (Sandbox Code Playgroud)
作品:
/custom/config/*
!/custom/config/foo.yml.dist
Run Code Online (Sandbox Code Playgroud)
这对我有用,我只想在回购中提交一个Cordova插件:
...
plugins/*
!plugins/cordova-plugin-app-customization
Run Code Online (Sandbox Code Playgroud)
我处理此问题的最简单方法是强制添加文件。即使它被埋藏或嵌套在 git 忽略的子目录树中,它也会在 git 中被考虑。
例如:
x64 文件夹被排除在 .gitignore 中:
x64/
Run Code Online (Sandbox Code Playgroud)
myFile.py但您想包含位于目录中的文件x64/Release/。那么你必须:
git add -f x64/Release/myFile.py
Run Code Online (Sandbox Code Playgroud)
您可以对匹配模式的多个文件执行此操作,例如
git add -f x64/Release/myFile*.py
Run Code Online (Sandbox Code Playgroud)
等等。
我有来自凉亭的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
Run Code Online (Sandbox Code Playgroud)
最小化的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
Run Code Online (Sandbox Code Playgroud)
希望有人能发现这个有用.
我尝试了上面给出的所有答案,但没有一个对我有用.在阅读了gitignore文档(这里)之后,我发现如果你首先排除了一个文件夹,那么子文件夹中的文件名就没有被索引.因此,如果您之后使用感叹号来包含文件,则它在索引中找不到,因此不会包含在您的git客户端中.
这是找到解决方案的方法.我开始为我的文件夹树中的所有子文件夹添加例外以使其正常工作,这是一个很糟糕的工作.之后我能够将详细配置压缩到下面的配置,这与文档有点相反.
工作.gitignore:
# Ignore the 'Pro' folder, except for the '3rdparty' subfolder
/Pro/*
!Pro/3rdparty/
# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/
# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
Run Code Online (Sandbox Code Playgroud)
结果我在我的git客户端中看到,只有Pro/3rdparty/domain/modulename /文件夹中的两个文件正在进行下一次提交,这正是我想要的.
如果您需要将同一文件夹的多个子文件夹列入白名单,请将排除语句下方的感叹号行分组,如下所示:
# Ignore the 'Pro' folder, except for the '3rdparty' subfolder
/Pro/*
!Pro/3rdparty/
# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/
# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/
Run Code Online (Sandbox Code Playgroud)
否则它不会按预期工作.
我知道我参加晚会很晚,但这是我的答案。
正如@Joakim所说,要忽略文件,可以使用以下内容。
# Ignore everything
*
# But not these files...
!.gitignore
!someFile.txt
Run Code Online (Sandbox Code Playgroud)
但是,如果文件位于嵌套目录中,则手动编写规则会有点困难。
例如,如果我们要跳过git项目中的所有文件,而不是a.txt中的文件aDir/anotherDir/someOtherDir/aDir/bDir/cDir。然后,我们.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
Run Code Online (Sandbox Code Playgroud)
上面给定的.gitignore文件将跳过除目录之外的所有目录和文件!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
您可能已经注意到,很难定义这些规则。
为了解决这个问题,我创建了一个名为git-do-not-ignore的简单控制台应用程序,它将为您生成规则。我主持的项目github上有详细的说明。
java -jar git-do-not-ignore.jar "aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt"
Run Code Online (Sandbox Code Playgroud)
!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
Run Code Online (Sandbox Code Playgroud)
谢谢。
这是我的方法:
# Ignore everything
*
# Whitelist anything that's a directory
!*/
# Whitelist some files
!.gitignore
# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**
# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules
# Ignore this file recursively
**/.DS_Store
Run Code Online (Sandbox Code Playgroud)
用于gig status -u递归地查看未跟踪目录中的单个文件- git status您只会看到文件夹,这可能使您误以为它们中的所有内容都已被跟踪
这就是我保持文件夹结构同时忽略其他所有内容的方式。您必须在每个目录(或 .gitkeep)中有一个 README.md 文件。
/data/*
!/data/README.md
!/data/input/
/data/input/*
!/data/input/README.md
!/data/output/
/data/output/*
!/data/output/README.md
Run Code Online (Sandbox Code Playgroud)
如果您需要忽略除少数文件和少数根文件夹以外的所有内容,则为简单的解决方案:
/*
!.gitignore
!showMe.txt
!my_visible_dir
Run Code Online (Sandbox Code Playgroud)
魔术已经存在/*(如上所述),它会(而不是递归)忽略(根)文件夹中的所有内容。
我得到了这个工作
# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
418791 次 |
| 最近记录: |