windows - .gitignore 任何级别的 NuGet 包文件夹,但包括任何级别的 .targets 文件

标签 windows git nuget gitignore

我正在尝试为 NuGet Package Restore Issues 实现解决方法.

这涉及忽略层次结构中任何级别的所有包文件夹的内容。但是,不应忽略 .targets 文件(通常位于包文件夹的子文件夹中)。

例如:

.gitignore
YourProject/
  YourProject.csproj
  packages/
    repositories.config
    Microsoft.Bcl.Build.1.0.7/
      lib/
        foo.dll
      tools/
        Microsoft.Bcl.Build.targets

Should include only files:

  • .gitignore
  • YourProject.csproj
  • Microsoft.Bcl.Build.targets

Windows, git version 1.8.1.msysgit.1.

I have read several answers on StackOverflow, checked the man page, and tried numerous variants without success.

This does not work:

packages/*
!*.targets

也不是这个:

packages/*
!packages/**/*.targets

也不是这个:

**/packages/*
!**/packages/**/*.targets

更新:

  • 无论包文件夹在层次结构中的什么位置,这都需要起作用。
  • 需要忽略包、子文件夹、子子文件夹等下的文件。
  • 其他 .gitignore 行,如 bin/,必须继续工作。

最佳答案

没有“好的”方法来做到这一点(请参阅 manpage 作为证明),但是有一个破解方法。

忽略 bat 的 packages/ 的主要问题是由于目录被忽略,git 甚至不检查它的子目录。

这里的解决方案 - 你必须有 2 个 gitignore 文件。一个是常规的,一个在 packages 目录中,如下所示:

# ignore all files
*
# do not ignore directories
!*/
# do not ignore targets
!*.targets
# do not ignore gitignore
!*.gitignore

新例子:

$ mkdir foo
$ cd foo/
$ mkdir foo
$ mkdir foo/bar
$ mkdir foo/bar/baz
$ touch foo/.foo
$ touch foo/bar/.foo
$ touch foo/bar/baz/.foo
$ touch regular.txt
$ touch foo/ignored.txt
$ touch foo/bar/baz/ignored-2.txt
$ cat foo/.gitignore
*
!*/
!*.foo
!regular.txt
!.gitignore
$ git add . && git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   foo/.foo
#       new file:   foo/bar/.foo
#       new file:   foo/bar/baz/.foo
#       new file:   regular.txt
#

关于windows - .gitignore 任何级别的 NuGet 包文件夹,但包括任何级别的 .targets 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17153152/

相关文章:

nuget - 为什么在 nuget.org 上没有列出包支持的框架?

nuget - 指定nuget包源

php - 是否可以在 Windows 中的 PHP 和 C 之间共享内存?

windows - 为什么在这种情况下批处理文件中的延迟扩展不起作用?

java.io.Windows 文件系统?

windows - 添加特定于应用程序的路径,因此它可以从 Windows 中的命令行运行

ruby-on-rails-3 - 如何使用之前的 Git check out 、在 Rails 3 中使用正确的文件和迁移干净地重新启动

Xcode 6.1 对 git 的表现很奇怪

git - 比较远程 Git 分支

asp.net-core - 在 Visual Studio 之外获取 .NET Core 中所有使用过的 NuGet 包的许可证信息