.gitignore NuGet packages folder at any level but include .targets file at any level

送分小仙女□ 提交于 2020-01-12 07:15:10

问题


I'm trying to implement the workaround for the NuGet Package Restore Issues.

This involves ignoring the contents of all packages folders, at any level in the hierarchy. However, .targets files (typically in subfolders of packages folders) should not be ignored.

For example:

.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

Nor this:

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

Nor this:

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

Update:

  • This needs to work regardless of where the packages folder is at in the hierarchy.
  • It needs to ignore files under packages, subfolders, sub-subfolders, etc.
  • Other .gitignore lines, like bin/, must continue to work.

回答1:


There is no 'good' way to do it (see manpage as a proof), however there is a hack around it.

Main issue with ignoring packages/ of the bat is that git does not even check it's subdirectories due to directory being ignored.

Solution here - you will have to have 2 gitignore files. One regular, and one inside the packages directory that looks like this:

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

New example:

$ 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
#



回答2:


The precedence of ignored paths is important, I take it.

I fixed this problem by specifying first what to keep then to ignore the rest of packages's content.

In .gitignore file make sure you should have:

!packages/repositories.config
packages/

See https://github.com/danyandresh/Research/commit/65291219a1c1fbcdb2216c686767e8c46451baa1

Please note my .gitignore file is a simple copy of Visual studio ignore list as found on github



来源:https://stackoverflow.com/questions/17153152/gitignore-nuget-packages-folder-at-any-level-but-include-targets-file-at-any-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!