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

不想你离开。 提交于 2019-12-03 12:32:19
Ruslan Osipov

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
#

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

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