使 .gitignore 忽略除少数文件之外的所有内容
据我所知,.gitignore
文件隐藏了 Git 版本控制中的指定文件。
如何告诉 .gitignore
忽略除我使用 Git 跟踪的文件之外的所有内容? 就像是:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
I understand that a .gitignore
file cloaks specified files from Git's version control.
How do I tell .gitignore
to ignore everything except the files I'm tracking with Git? Something like:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
如果您想忽略目录中除其中一个文件之外的全部内容,您可以为文件路径中的每个目录编写一对规则。 例如
.gitignore
忽略pippo
文件夹,除了pippo/pluto/paperino.xml
请注意,
/*
是文件夹所必需的。 以下内容不会起作用:但只有这个会起作用:
请注意,如果您只是在上面编写:
它将不起作用,因为 Git 不存在中间
pluto
文件夹,因此paperino.xml
找不到存在的地方。If you want to ignore the whole content of a directory except one file inside it, you could write a pair of rules for each directory in the file path. E.g.
.gitignore
to ignore thepippo
folder except frompippo/pluto/paperino.xml
Note that the
/*
is required for the folders. The following will not work:but only this will:
Note that if you simply had written above:
It wouldn't work because the intermediary
pluto
folder would not exist to Git, sopaperino.xml
could not find a place in which to exist.在大多数情况下,您希望使用
/*
而不是*
或*/
使用
* 有效,但它递归地工作。 从那时起它就不会再查看目录了。 人们建议再次使用
!*/
来包含目录,但实际上最好使用/*
将最高级别的文件夹列入黑名单上面的代码将忽略除
之外的所有文件.gitignore
、README.md
、folder/a/file.txt
、folder/a/b1/
和/a/b2/
以及最后两个文件夹中包含的所有内容。 (并且.DS_Store
和*.log
文件在这些文件夹中将被忽略。)显然我可以这样做,例如
!/folder
或!/.gitignore 也是如此。
更多信息:http://git-scm.com/docs/gitignore
You want to use
/*
instead of*
or*/
in most casesUsing
*
is valid, but it works recursively. It won't look into directories from then on out. People recommend using!*/
to includelist directories again, but it's actually better to blocklist the highest level folder with/*
The above code would ignore all files except for
.gitignore
,README.md
,folder/a/file.txt
,folder/a/b1/
andfolder/a/b2/
and everything contained in those last two folders. (And.DS_Store
and*.log
files would be ignored in those folders.)Obviously I could do e.g.
!/folder
or!/.gitignore
too.More info: http://git-scm.com/docs/gitignore
更具体一点:
示例:忽略
webroot/cache
中的所有内容 - 但保留webroot/cache/.htaccess
。请注意
cache
文件夹后面的斜杠 (/):FAILS
WORKS
A little more specific:
Example: Ignore everything in
webroot/cache
- but keepwebroot/cache/.htaccess
.Notice the slash (/) after the
cache
folder:FAILS
WORKS
让我们工具化吧!
正如@Joakim 所说,要忽略文件,您可以使用如下所示的内容。
但如果文件位于嵌套目录中,则编写这些规则有点困难。
例如,如果我们想跳过位于
aDir/anotherDir/someOtherDir/aDir/bDir/cDir
中的所有文件,但不跳过a.txt
。然后,我们的
.gitignore
将是这样的,这很难用手写。
为了解决这个障碍,我创建了一个名为 git-do-not-ignore 的 Web 应用程序,它将为您生成规则。
工具
演示
示例输入
示例输出
Let's toolify!
As @Joakim said, to ignore a file, you can use something like below.
but if the file is in nested directories, it's a little difficult to write those rules.
For example, if we want to skip all files but not
a.txt
, located inaDir/anotherDir/someOtherDir/aDir/bDir/cDir
.Then, our
.gitignore
will be something like thisThis is quite hard to write by hand.
To solve this hurdle, I've created a web app named git-do-not-ignore which will generate the rules for you.
Tool
Demo
Sample Input
Sample Output
与 OP 有类似的问题,但前 10 个赞成的答案都没有真正起作用。
我终于发现了以下
错误语法:
正确语法:
来自gitignore 手册页:
这意味着上面的“错误语法”是错误的,因为
bin/script.sh
无法重新包含,因为bin/
被忽略。 就这样。扩展示例:
$ 树 .
$ cat .gitignore
$ git status --untracked-files --ignored
Had the similar issue as OP but none of top 10 upvoted answer actually worked.
I finally found out the following
Wrong syntax :
Correct syntax :
Explanation from gitignore man page :
Which means that "Wrong syntax" above is wrong because
bin/script.sh
cannot be reincluded asbin/
is ignored. That's all.Extended example :
$ tree .
$ cat .gitignore
$ git status --untracked-files --ignored
要忽略目录中的某些文件,您必须按照正确的顺序执行此操作:
例如,忽略文件夹“application”中除 index.php 和文件夹“config”之外的所有内容注意顺序。
你必须首先否定你想要的东西。
失败
application/*
!application/config/*
!application/index.php
有效
!application/config/*
!application/index.php
application/*
To ignore some files in a directory, you have to do this in the correct order:
For example, ignore everything in folder "application" except index.php and folder "config" pay attention to the order.
You must negate want you want first.
FAILS
application/*
!application/config/*
!application/index.php
WORKS
!application/config/*
!application/index.php
application/*
您可以使用 git config status.showUntrackedFiles no ,所有未跟踪的文件将对您隐藏。 有关详细信息,请参阅
man git-config
。You can use
git config status.showUntrackedFiles no
and all untracked files will be hidden from you. Seeman git-config
for details.这就是我保持文件夹结构而忽略其他所有内容的方法。 每个目录(或 .gitkeep)中必须有一个 README.md 文件。
This is how I keep the structure of folders while ignoring everything else. You have to have a README.md file in each directory (or .gitkeep).
要从 .gitignore 中排除文件夹,可以执行以下操作。
这将忽略
bower_components
内的所有文件/子文件夹(/highcharts
除外)。To exclude folder from .gitignore, the following can be done.
This will ignore all files/subfolders inside
bower_components
except for/highcharts
.关于此有很多类似的问题,所以我将发布我之前写的内容:
我让它在我的机器上工作的唯一方法是这样做:
注意你如何必须明确允许你的每个级别的内容想要包括在内。 因此,如果我在主题下有 5 个子目录,我仍然需要将其拼写出来。
这是来自 @Yarin 的评论:https://stackoverflow.com/a/5250314/1696153
这些都是有用的主题:
我也尝试过
**/wp-content/themes/**
或
/wp-content/themes/**/*
这些都不适合我。 很多线索和错误!
There are a bunch of similar questions about this, so I'll post what I wrote before:
The only way I got this to work on my machine was to do it this way:
Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories 5 deep under themes, I still need to spell that out.
This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153
These were useful topics:
I also tried
and
**/wp-content/themes/**
or
/wp-content/themes/**/*
None of that worked for me, either. Lots of trail and error!
我就是这样做的:
使用
gig status -u
递归地查看未跟踪目录中的单个文件 - 使用git status
你只能看到文件夹,这可能会欺骗你认为他们里面的一切都被追踪This is how I did it:
Use
gig status -u
to view individual files in untracked directories recursively - withgit status
you'd only see folders, which could fool you into thinking that everything inside them was tracked我的子文件夹有问题。
不起作用:
起作用:
I had a problem with subfolder.
Does not work:
Works:
不确定是否已经指出了这一点,但我在使用我希望 gitignore 忽略的文件夹前面的
!
忽略被忽略文件夹内的子文件夹时遇到了麻烦。但是我发现被忽略的文件夹的路径中没有
*
。 我的 .gitignore 看起来像这样:子文件夹仍然被忽略,但在文件夹后添加
*
使它像这样工作Not sure if this has been pointed out already but I was having trouble ignoring a subfolder inside an ignored folder using the
!
in front of the folder i want gitignore to ignore.However I figured out that the folder that was being ignored had no
*
in its path. My .gitignore looked like this:The subfolder was still being ignored, but adding a
*
after folder made it work like so我尝试了上面给出的所有答案,但没有一个对我有用。 阅读 gitignore 文档(此处)后,我发现如果您首先排除某个文件夹,则文件名子文件夹中的内容未被索引。 因此,如果您随后使用感叹号来包含文件,则在索引中找不到该文件,因此不会包含在您的 git 客户端中。
这就是找到解决方案的方法。 我首先为文件夹树中的所有子文件夹添加例外以使其正常工作,这是一项艰巨的工作。 之后我能够将详细配置压缩到下面的配置,这与文档有点相反。
工作 .gitignore:
结果我在我的 git 客户端中看到只有 Pro/3rdparty/domain/modulename 中的两个文件/ 文件夹正在为下一次提交而准备,这正是我正在寻找的。
如果您需要将同一文件夹的多个子文件夹列入白名单,则将排除语句下方的感叹号行分组,如下所示:
否则它将无法按预期工作。
I tried all answers as given here above, but none worked for me. After reading the gitignore documentation (here) i found out that if you exclude a folder first that the filenames in the subfolder are not being indexed. So if you use the exclamation mark afterwards to include a file, it is not found in the index and thus not being included in your git client.
That was the way to finding the solution. I started with adding exceptions for all subfolders in my folder tree to get it working, which is a hell of a job. Afterwards i was able to compact the detailed configuration to the configuration below, which is a bit contrary to the documentation..
Working .gitignore:
As result i see in my git client that only the two files inside the Pro/3rdparty/domain/modulename/ folder are being staged for the next commit, and that was exactly what i was looking for.
And if you need to whitelist several subfolders of the same folder then group the exclamation mark lines below the exclude statement like this:
Else it wont work as expected.
这里有很多复杂的答案......
这是我在上面没有看到的一些非常简单的东西,并且在许多情况下都能很好地工作:
这不会忽略任何无扩展名文件,但是如果您有一堆具有相同或相似名称的文件,您可以为这些文件添加排除项
a lot of complex answers here...
here's something pretty simple that I don't see above and works well in many scenarios:
this doesn't ignore any extensionless files, but if you have a bunch that all have the same or similar name, you can add exclusions for those
这对我有用,我只想将一个 Cordova 插件提交到存储库:
That's what have worked for me, I wanted to commit only one Cordova plugin to the repo:
我有来自 Bower 的 Jquery 和 Angular。 Bower 将它们安装在
最小化的 jquery 位于
dist
目录中,而 Angular 位于angular
目录中。 我只需要最小化的文件即可提交到 github。 对 .gitignore 进行了一些篡改,这就是我设法想到的......希望有人会发现这很有用。
I have Jquery and Angular from bower. Bower installed them in
The minimized jquery is inside the
dist
directory and angular is insideangular
directory. I only needed minimized files to be commited to github. Some tampering with .gitignore and this is what I managed to conjure...Hope someone could find this useful.
我解决此问题的最简单方法是强制添加文件。 即使它被埋藏或嵌套在 git 忽略的子目录树中,它也会在 git 中被考虑。
例如:
x64 文件夹被排除在 .gitignore 中:
但您想要包含位于
x64/Release/
目录中的文件myFile.py
。然后你必须:
你可以对匹配模式的多个文件执行此操作,例如
等等。
The simplest way that I go about this is to force add a file. It will be accounted for in git even if it is buried or nested inside a git-ignored subdirectory tree.
For example:
x64 folder is excluded in .gitignore:
But you want to include the file
myFile.py
located inx64/Release/
directory.Then you have to:
You can do this for multiple files of files that match a pattern e.g.
and so on.
如果您需要忽略除少数文件和少数 root 文件夹之外的所有内容,则可以使用简单的解决方案:
神奇之处在于
/*
(如上所述),它会忽略(根)文件夹中的所有内容但不是递归的。Simple solution if you need to ignore everything except few files and few root folders:
The magic is in
/*
(as described above) it ignores everything in the (root) folder BUT NOT recursively.我得到了这个工作
I got this working
到目前为止,没有任何东西对我有用,因为我试图从库中添加一个罐子。
这不起作用:
这起作用了:
Nothing worked for me so far because I was trying to add one jar from lib.
This did not worked:
This worked:
我在否定单个文件方面也遇到了一些问题。 我能够提交它们,但我的 IDE (IntelliJ) 总是抱怨被跟踪的忽略文件。
显示了我以这种方式排除的两个文件:
最后,这对我有用:
关键是文件夹的否定
typo3conf/< /代码> 首先。
此外,似乎语句的顺序并不重要。 相反,您需要先显式否定所有子文件夹,然后才能否定其中的单个文件。
对于 .gitignore,文件夹
!public/typo3conf/
和文件夹内容public/typo3conf/*
是两个不同的东西。很棒的线索! 这个问题困扰了我一段时间;)
I also had some issues with the negation of single files. I was able to commit them, but my IDE (IntelliJ) always complained about ignored files, which are tracked.
Displayed the two files, which I've excluded this way:
In the end, this worked for me:
The key was the negation of the folder
typo3conf/
first.Also, it seems that the order of the statements doesn't matter. Instead, you need to explicitly negate all subfolders, before you can negate single files in it.
The folder
!public/typo3conf/
and the folder contentspublic/typo3conf/*
are two different things for .gitignore.Great thread! This issue bothered me for a while ;)
我似乎找到了一些对我有用但没有人提到的东西。
基本上,它似乎否定了子目录被忽略,您必须有两个条目,一个用于子目录本身
!subdir/
,然后另一个条目扩展到下的所有文件和文件夹它!subdir/**/*
I seem to have found something that worked for me which no one else mentioned.
Basically, it seems to negate a sub-directory from being ignored, you have to have two entries, one for the sub-directory itself
!subdir/
and then another one which expands to all files and folders under it!subdir/**/*
这是一场灾难,在使用
git
16 年(2005 年)之后,没有简单明了的方法来排除位于深处的一个文件。目录层次结构,否则应被忽略。 相反,我们需要诉诸疯狂的事情,例如反复挖掘结构并以正确的顺序排除和包含目录。 一年要做四次的事情是绝对不可能记住的。唯一聪明但非常奇怪的选择是使用 git add --force 。 当你不单独处理 gitted 存储库时,某些事情肯定会在某个时候失败。
所以我写了一个小的 bash 函数来为你解决这个问题,以便于复制粘贴。 让我首先解释一下这一行:
描述
if
语句来调整第一个项目没有得到/
。tr '\/' '\n'
- 将标准 POSIX 路径中的/
转换为\n
(以获取列表)y="$y/$x"
- 在上一个目录之后添加下一个子目录(在列表中)。echo -e '!'"$y/\n$y/*"
-打印 2 行子目录项,第一行使用
!
prefix,第二行使用/*
postfix。sed '$d'
- 删除最后一行sed -z 's/..$//'
- 删除最后 2 个字符/*
最后一行然后我们创建函数:
这里我将算术
if
语句简化为if [[ f -eq 0 ]];
。享受吧!
It's a disaster that after 16 years (2005) of using
git
, there is no simple and obvious way to exclude one file when located deep in a directory hierarchy, that should otherwise be ignored. Instead we need to resort to crazy things like repeatedly digging down the structure and excluding and including directories in the correct order. Something which is darn right impossible to remember those 4 times a year you need to do it.The only smart, but very weird alternative, is to use the
git add --force
. Something which is bound to fail at some point, when you're not working on the gitted repo alone.So I wrote a small bash function to fix this for you, for easy copy paste. Let me first explain the one liner:
Description
if
statement to adjust for the 1st item not getting a/
.tr '\/' '\n'
- translate the/
to\n
in standard POSIX paths (to get a list)y="$y/$x"
- add the next sub-dirctory (in list) after the previous directory.echo -e '!'"$y/\n$y/*"
-print the 2 lines of sub-directory item, 1st with
!
prefix, and 2nd with/*
postfix.sed '$d'
- remove last linesed -z 's/..$//'
- remove last 2 characters/*
from last lineThen we create the function:
Here I simplified the arithmetic
if
statement toif [[ f -eq 0 ]];
.Enjoy!
上述片段效果很好,但有点令人困惑。 我通过失败和尝试找到了正确的方法。 这是我如何让它工作的;
example_PostProcessingManager
的文件夹,example_PostProcessingManager/src/
文件夹内容之外的所有内容example_PostProcessingManager/bin/data/
文件夹内容简而言之,我想保留
data
和src
文件夹的内容,并排除example_PostProcessingManager
文件夹中的其他内容为此,我编辑 <代码>.gitignore如下;
PS主要思想是您需要从根文件夹开始并逐步访问目标文件夹。 如果您按如下方式执行此操作,它将仅包含
src
文件夹,并忽略data
文件夹中的所有内容。失败案例
The aforementioned snippets work well but they are a little bit confusing. I found the right way with fail and try. Here is how I manage it to work;
example_PostProcessingManager
example_PostProcessingManager/src/
folder contentsexample_PostProcessingManager/bin/data/
folder contentsBriefly, I want to keep the contents of
data
andsrc
folders and exclude anything else inside theexample_PostProcessingManager
folderTo do that I edit the
.gitignore
as follows;P.S. the main idea is you need to start from root folder and access the destination folder step by step. If you do it as follows, it will only include the
src
folder and ignores everything inside thedata
folder.FAILED CASE