Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 393
TrueWill Avatar asked Jun 17 '13 17:06

TrueWill


1 Answers

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
#
like image 70
Ruslan Osipov Avatar answered Oct 07 '22 05:10

Ruslan Osipov