.gitignore NuGet在任何级别打包文件夹,但在任何级别都包含.targets文件

Tru*_*ill 11 windows git gitignore nuget

我正在尝试实现NuGet包还原问题的解决方法.

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

例如:

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

应该只包含文件:

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

Windows,git版本1.8.1.msysgit.1.

我已经在StackOverflow上阅读了几个答案,检查了手册页,并尝试了许多变种而没有成功.

这不起作用:

packages/*
!*.targets
Run Code Online (Sandbox Code Playgroud)

这不是:

packages/*
!packages/**/*.targets
Run Code Online (Sandbox Code Playgroud)

这不是:

**/packages/*
!**/packages/**/*.targets
Run Code Online (Sandbox Code Playgroud)

更新:

  • 无论package文件夹位于层次结构中的哪个位置,这都需要工作.
  • 它需要忽略包,子文件夹,子子文件夹等下的文件.
  • 其他.gitignore行,bin/必须继续工作.

Rus*_*pov 8

没有"好"的方法(参见手册作为证据),但是它周围有一个黑客.

忽略packages/bat的主要问题是git甚至没有检查它的子目录,因为目录被忽略了.

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

# ignore all files
*
# do not ignore directories
!*/
# do not ignore targets
!*.targets
# do not ignore gitignore
!*.gitignore
Run Code Online (Sandbox Code Playgroud)

新例子:

$ 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
#
Run Code Online (Sandbox Code Playgroud)