.gitignore排除不适用于单个文件 [英] .gitignore exclusion is not working for a single file

查看:179
本文介绍了.gitignore排除不适用于单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ git --version 
git版本2.6.4

我意识到这是重复的,但另一个问题的答案没有帮助。

文件夹结构

  / 
- dist /
---- samples /
---- README.md
---- foo /
---- bar /
---- baz .js

除了samples和README.md,我想忽略dist中的所有内容。我的第一次尝试没有奏效,所以我决定只是忽略README:

  dist 
! dist / samples / README.md

README是一个全新的文件。 dist文件夹根本不在源代码控制中。但是,当我检查忽略时,它仍然被忽略:

  $ git check-ignore dist / samples / README.md 
dist / samples / README.md
$ git check-ignore src
#空行

我试过:


  1. 更改忽略顺序
  2. 删除从行首开始斜杠

  3. 在中间添加**: dist / ** / README.md

  4. 在第一行添加一个 / * dist 的结尾

唯一被忽略的其他内容是* .js,* .js.map,temp和node_modules。有趣的是,Webstorm认为文件不会被忽略(它会改变颜色),但命令行工具确实认为它被忽略。



我不明白我做错了什么。该模式看起来很简单:


  1. 忽略此目录中的所有内容

  2. 不会忽略这里有一件事

但这显然不起作用。

解决方案

问题仅仅在于你忽略了目录 dist 。因此,Git将不再查看目录以查找其他文件。



这个相关答案,您需要将目录列入白名单,并且只会忽略其内容。因为你有一个嵌套结构,所以最终确实有些复杂:

 #忽略`dist`中的所有内容文件夹
dist / *
#...但不要忽略`dist / samples`文件夹
!dist / samples /
#...但忽略dist / samples内的所有内容`文件夹
dist / samples / *
#...除了`README.md`那里
!dist / samples / README.md

这也在 gitignore文档(强调我的):


可选的前缀,否定模式;任何先前模式排除的匹配文件将再次包含在内。 如果排除该文件的父目录,则无法重新包含文件。出于性能原因,Git未列出排除的目录,因此,包含的文件上的任何模式都无效,无论在哪里定义。


$ git --version
git version 2.6.4

I realize this is a duplicate but the other question's answers have not helped.

Folder structure

/
-- dist/
---- samples/ 
---- README.md
---- foo/
---- bar/
---- baz.js 

I want to ignore everything in dist except samples and README.md. My first few tries at it didn't work, so I settled on just unignoring the README:

dist
!dist/samples/README.md 

README is a brand new file. The dist folder is not in source control at all. But when I check ignore, it's still being ignored:

$ git check-ignore dist/samples/README.md 
dist/samples/README.md
$ git check-ignore src
   # empty line    

I've tried:

  1. changing the order of the ignores
  2. removing the slash from the beginning of the line
  3. adding a ** in the middle: dist/**/README.md
  4. adding a /* to the end of dist on the first line

The only other things being ignored are *.js, *.js.map, temp and node_modules. The funny thing is that Webstorm thinks that the file is not being ignored (it changes color) but the command line tool does think it is being ignored.

I don't see what I'm doing wrong. The pattern seems very simple:

  1. ignore all the things in this directory
  2. don't ignore this one thing here

But it's clearly not working.

解决方案

The problem is simply because you are ignoring the directory dist. So Git will no longer look into the directory to look for other files.

As explained in this related answer, you need to whitelist the directory and only ignore its contents. Since you are having a nested structure, this does end up being a bit complicated though:

# ignore everything in the `dist` folder
dist/*
# … but don’t ignore the `dist/samples` folder
!dist/samples/
# … but ignore everything inside that `dist/samples` folder
dist/samples/*
# … except the `README.md` there
!dist/samples/README.md

This is also explicitly mentioned in the gitignore documentation (emphasis mine):

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

这篇关于.gitignore排除不适用于单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆