【问题标题】:git exclude specific folder and it's content from gitignore rule [duplicate]git排除特定文件夹及其来自gitignore规则的内容[重复]
【发布时间】:2017-05-21 08:00:06
【问题描述】:

我的项目中有一个 .gitignore 文件,其中包含以下规则:

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
[Xx]64/
[Xx]86/
[Bb]uild/
bld/
[Bb]in/
[Oo]bj/

我的应用程序中有一个使用关键字“Build”的特定文件夹,但我想将这个特定文件夹及其文件包含在我的提交中。

我要包含的路径如下:

Views/Build/Index.cshtml
Views/Build/Create.cshtml

我尝试了以下方法,但没有成功:

!Views/[Bb]uild/*

我做错了什么?

我正在努力寻找问题的答案,希望有人能提供帮助。

【问题讨论】:

  • 顺便说一句。您可以在您的忽略规则前面加上 / 以在根处应用,例如当构建输出只构建到根文件夹中时。
  • 顺便说一句。 (2),Git 只会忽略 Git 尚未跟踪的文件,因此您始终可以使用 git add -f Views/Build/Index.cshtml 显式添加文件,即使该文件实际上被忽略(这将有效地关闭该文件的忽略规则,所以随后的更改也将被正确检测到)。

标签: git


【解决方案1】:

!Views/[Bb]uild 添加到底部的 .gitignore 文件中(在其他规则之后)。我已经使用以下文件对此进行了测试:

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
[Xx]64/
[Xx]86/
[Bb]uild/
bld/
[Bb]in/
[Oo]bj/

!Views/[Bb]uild

你的尝试和我的不同之处在于你添加了!Views/[Bb]uild/*,而我添加了!Views/[Bb]uild

我相信!Views/[Bb]uild/* 不起作用的原因是因为它说“排除 'Views/Build' 下的所有内容不被忽略”,但这本身没有效果,因为目录本身仍然被忽略。我的规则不包括目录本身(以及它下面的所有内容)。

编辑

这是我测试过的目录的树结构:

$ tree -a -I .git
.
├── .gitignore
├── hello.cs
├── temp.py
├── temp.txt
├── temp.vi
└── Views
    └── Build
        ├── Create.cshtml
        └── Index.cshtml

这是git status的输出:

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        Views/
        hello.cs
        temp.py
        temp.txt
        temp.vi

【讨论】:

  • 我已经按照您的建议进行了尝试,但仍然不满意。运行 git check-ignore 后,似乎仍然应用了全局规则:$ git check-ignore -v XXX/Views/Build/Status.cshtml .gitignore:20:[Bb]uild/ XXX/Views/Build/Status.cshtml
  • @jgill09 我也从 git check-ignore 获得了该输出,但是如果您尝试取消暂存和重新暂存所有更改,您应该会看到它起作用 (git reset &amp;&amp; git add -A &amp;&amp; git status)
  • 完美,成功了。谢谢。
  • @jgill09 太好了。如果它解决了您的问题,如果您能接受答案,我将不胜感激。 :)
猜你喜欢
  • 2013-01-19
  • 2012-09-19
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 1970-01-01
  • 2017-02-01
  • 2017-12-21
相关资源
最近更新 更多