我有文件夹application /我添加到.gitignore.应用程序/文件夹内是文件夹application/language/gr.我该如何包含此文件夹?我试过这个
application/
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)
没有运气......
Chr*_*sen 1461
如果你排除application/
,那么它下面的所有东西将永远被排除(即使一些后来的否定排除模式("unignore")可能与下面的东西相匹配application/
).
要做你想做的事,你必须"unignore"任何你想要"unignore"的东西的父目录.通常,您最终会成对编写针对此情况的规则:忽略目录中的所有内容,但不忽略某些子目录.
# you can skip this first one if it is not already excluded by prior patterns
!application/
application/*
!application/language/
application/language/*
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)
注意
尾随/*
是重要的:
dir/
排除了一个名为dir
和(隐式)其下的所有内容的目录.dir/
,Git永远不会看任何东西dir
,因此永远不会将任何"非排除"模式应用于任何东西dir
.dir/*
对dir
自己一无所知; 它只是排除了一切dir
.有了dir/*
,Git将处理直接内容dir
,使其他模式有机会"取消排除"某些内容(!dir/sub/
).Von*_*onC 128
提交59856de从卡斯滕Blees(kblees) GIT中1.9/2.0(Q1 2014)阐明这种情况下:
gitignore.txt
:澄清排除目录的递归性质一个可选的前缀"
!
",它否定了模式; 之前模式排除的任何匹配文件将再次包含在内.如果排除该文件的父目录,则无法重新包含文件.(
*
)
(*
:除非在git 2.8+中满足某些条件,见下文)
Git没有列出出于性能原因排除的目录,因此所包含文件的任何模式都无效,无论它们在何处定义.对于以文字" " 开头的模式,
\
在第一个"!
" 前面加一个反斜杠(" ")!
,例如"\!important!.txt
".排除除特定目录之外的所有内容的示例
foo/bar
(注意/*
- 没有斜杠,通配符也会排除其中的所有内容foo/bar
):
--------------------------------------------------------------
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
--------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
在你的情况下:
application/*
!application/**/
application/language/*
!application/language/**/
!application/language/gr/**
Run Code Online (Sandbox Code Playgroud)
在能够列出给定文件夹中的文件之前,必须先将文件夹列入白名单.
2016年2月/ 3月更新:
请注意,使用git 2.9.x/2.10(2016年中期?),如果重新包含的路径中没有通配符,则可以重新包含该文件(如果排除该文件的父目录).
NguyễnTháiNgọcDuy(pclouds
)正在尝试添加此功能:
所以使用git 2.9+,这可能实际上有效,但最终还原了:
application/
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)
rpy*_*yzh 49
@Chris Johnsen的答案很棒,但是对于更新版本的Git(1.8.2或更高版本),你可以使用双星号模式来获得更简便的解决方案:
# assuming the root folder you want to ignore is 'application'
application/**/*
# the subfolder(s) you want to track:
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)
这样,您就不必"unignore"要跟踪的子文件夹的父目录.
使用Git 2.17.0(不确定在此版本之前的早期版本.可能回到1.8.2),使用**
模式结合排除每个子目录导致文件的工作.例如:
# assuming the root folder you want to ignore is 'application'
application/**
# Explicitly track certain content nested in the 'application' folder:
!application/language/
!application/language/gr/
!application/language/gr/** # Example adding all files & folder in the 'gr' folder
!application/language/gr/SomeFile.txt # Example adding specific file in the 'gr' folder
Run Code Online (Sandbox Code Playgroud)
Kat*_*tie 17
关于这一点有很多类似的问题,所以我会发布我之前写过的内容:
我在机器上工作的唯一方法是这样做:
# 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:
!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/**/*
这些也不适用于我.很多线索和错误!
小智 14
我想跟踪 Nagios 配置文件/etc/nagios/
以及/usr/lib64/nagios/plugins/
. 为此,我初始化了一个 git 存储库/
并使用了以下排除列表:
/*
!etc
etc/*
!etc/nagios
!usr
usr/*
!usr/lib64
usr/lib64/*
!usr/lib64/nagios
usr/lib64/nagios/*
!usr/lib64/nagios/plugins
Run Code Online (Sandbox Code Playgroud)
Git 像这样沿着列表走下去:
/* exclude everything under / ...
!etc but include /etc back
etc/* exclude everything under /etc/...
!etc/nagios but include /etc/nagios back
!usr but include /usr back
usr/* exclude everything under /usr/...
and so on...
Run Code Online (Sandbox Code Playgroud)
Mak*_*min 12
我的 JetBrains IntelliJ IDEA.gitignore
配置,我需要排除整个.idea
文件夹,除了.idea/runConfigurations
:
.idea
!.idea/
.idea/*
!.idea/runConfigurations/
Run Code Online (Sandbox Code Playgroud)
小智 11
我发现只有这个确实有效.
**/node_modules/*
!**/node_modules/keep-dir
Run Code Online (Sandbox Code Playgroud)
D. *_*ble 10
最简单也可能是最好的方法是尝试手动添加文件(通常这优先于.gitignore
-style 规则):
git add /path/to/module
Run Code Online (Sandbox Code Playgroud)
-f
如果文件已被忽略,您可能需要。你甚至可以在-N
意图添加标志,建议您将它们添加,但不是马上。我经常为尚未准备好上演的新文件执行此操作。
这是发布在很容易成为重复 QA的答案的副本。我将它重新发布在这里是为了提高可见性——我发现不要把 gitignore 规则弄得一团糟更容易。
我在这里发现了一个类似的情况,默认情况下在 laravel 中,.gitignore
忽略所有使用 asterix,然后覆盖公共目录。(这也是与主要答案@Chris Johnsen 相同的解决方案,只是更薄更简洁。)
*
!public
!.gitignore
Run Code Online (Sandbox Code Playgroud)
如果您遇到 OP 场景,这还不够。
如果您要提交的特定子文件夹public
,譬如说例如,在你的public/products
目录要包括那些一个子文件夹深如文件,包括public/products/a/b.jpg
他们不会正确检测,即使你加他们特别喜欢这个!/public/products
,!public/products/*
等。
解决方案是确保为每个路径级别添加一个条目,以覆盖它们。
*
!.gitignore
!public/
!public/*/
!public/products/
!public/products/*
!public/products/*/
!public/products/*/
!public/products/*/*
Run Code Online (Sandbox Code Playgroud)
gitignore - 指定要忽略的故意未跟踪的文件。
排除除特定目录foo/bar之外的所有内容的示例(注意/* - 没有斜杠,通配符也会排除foo/bar中的所有内容):
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
Run Code Online (Sandbox Code Playgroud)
WordPress的另一个例子:
!/wp-content
wp-content/*
!/wp-content/plugins
wp-content/plugins/*
!wp-content/plugins/my-awesome-plugin
Run Code Online (Sandbox Code Playgroud)
更多信息在这里: https: //git-scm.com/docs/gitignore
小智 6
这对我有用:
**/.idea/**
!**/.idea/copyright/
!.idea/copyright/profiles_settings.xml
!.idea/copyright/Copyright.xml
Run Code Online (Sandbox Code Playgroud)
因此,由于许多程序员都使用node。满足此问题的用例是排除node_modules
一个模块module-a
,例如:
!node_modules/
node_modules/*
!node_modules/module-a/
Run Code Online (Sandbox Code Playgroud)
添加其他答案:
!/.vs/ <== include this folder to source control, folder only, nothing else
/.vs/* <== but ignore all files and sub-folder inside this folder
!/.vs/ProjectSettings.json <== but include this file to source control
!/.vs/config/ <== then include this folder to source control, folder only, nothing else
!/.vs/config/* <== then include all files inside the folder
Run Code Online (Sandbox Code Playgroud)
结果如下:
归档时间: |
|
查看次数: |
324313 次 |
最近记录: |