94

I'm trying to create a .gitignore for a Visual Studio project that uses NuGet. It currently contains:

\packages/*
!packages/repositories.config

This does not ignore anything in the folder. Everything gets staged on an add. I have also tried:

packages/
!packages/repositories.config

This ignores everything in the packages folder and does not include the packages/repositories.config.

What am I doing wrong?

Dipesh
  • 1,263
  • 1
  • 11
  • 12

6 Answers6

116
/packages/
!packages/repositories.config

You can also add a .gitignore in the packages folder:

*
!repositories.config
!.gitignore
net_prog
  • 9,921
  • 16
  • 55
  • 70
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 15
    It *would* be a shame if the `.gitignore` ignored itself. +1 – Thomas Oct 12 '13 at 11:33
  • 2
    The first block does not work for me (git version 1.8.4.msysgit.0). I think the first line should just be `packages` without the star. – Grant Birchmeier Mar 14 '14 at 20:55
  • 1
    I found that Visual Studio still displayed the packages folder. I had to remove `ms-persist.xml` from the `.git` folder for the `.gitignore` file to be respected. See https://ericnelson.wordpress.com/2014/06/21/is-visual-studio-2013-ignoring-your-gitignore-file/ for more on this. – rudivonstaden Mar 26 '15 at 13:08
  • I don't know which version have you tried this on, but `/packages/` didn't work for me. Instead I found [this answer](http://stackoverflow.com/a/5534865) very helpfull. In short `packages/*` did the trick. Git version 2.6.1 – Stelios Adamantidis Oct 12 '16 at 00:02
44

I faced the same issue.

None of the above solutions worked for me. And I think it's a poor solution to maintain multiple .ignore files.

This is how I solved it.

**/packages/*
!**/packages/repositories.config

Combining two asterisks will match any string of folders. I thought leaving out asterisks would have the same effect, but apparently I (we) were wrong, as it doesn't seem to work.

The official .gitignore template for Visual Studio recommends the following solutions:

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config

EDIT: You can use https://www.gitignore.io to generate .ignore file for your favorite project :-)

Ali Reza Dehdar
  • 1,991
  • 1
  • 15
  • 17
  • 3
    This is the only variant that worked for me since I had multple projects in repository with packages folder in each. Thanks. – Michael Logutov Jul 23 '15 at 10:44
  • 1
    The one that works great is the recommended official template for Visual Studio + uncomment the last line. – vezenkov Nov 16 '15 at 12:33
  • 1
    This is actually a little risky. It ignores any folder named "packages", not just the one at the solution level. I found out too late that some of my model classes had never been part of the repository. – Rich Feb 11 '16 at 17:39
  • 2
    It has now been updated: # NuGet Packages *.nupkg # The packages folder can be ignored because of Package Restore **/packages/* # except build/, which is used as an MSBuild target. !**/packages/build/ # Uncomment if necessary however generally it will be regenerated when needed #!**/packages/repositories.config # NuGet v3's project.json files produces more ignoreable files *.nuget.props *.nuget.targets – Daniel Ryan Jan 17 '17 at 00:09
  • except that `!**/packages/build/` doesn't work for me, with the given solution... build folders are not commited – JobaDiniz Jan 31 '18 at 18:13
  • Plus one for mentioning the official solution. – Jpsy Jun 27 '18 at 07:38
  • This was the only one that worked for me. I looked through the .gitignore template for Visual Studio that was mentioned but must have missed this little bit. Thank you so much!! Plus one for sure. – Gharbad The Weak May 19 '23 at 21:31
12

This works for me.

#NuGet
packages
!packages/repositories.config

(Same as @manojlds's answer except removed the star in the first line. That didn't work for me.)

Community
  • 1
  • 1
Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
8

I found this simple pattern works.

/packages/*/

It should ignore all directories in the root packages directory, but include all files there. Not sure what other files than repositories.config might appear in there or whether they should be included in the repository.

See also .gitignore Syntax: bin vs bin/ vs. bin/* vs. bin/**

Community
  • 1
  • 1
ygoe
  • 18,655
  • 23
  • 113
  • 210
0

For me only this worked:

**/packages/**

Sam
  • 13,934
  • 26
  • 108
  • 194
0

If you want to do it correctly for ALL NUGET Temp artifacts

Add this code to your .gitignore


# Ignore all my NuGet temp Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
Transformer
  • 6,963
  • 2
  • 26
  • 52