.gitignore NuGet exclude packages/ include packages/repositories.config

GitNugetGitignore

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

/packages/
!packages/repositories.config

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

*
!repositories.config
!.gitignore

Solution 2 - Git

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 :-)

Solution 3 - Git

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.)

Solution 4 - Git

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 https://stackoverflow.com/q/8783093/143684

Solution 5 - Git

For me only this worked:

**/packages/**

Solution 6 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDipeshView Question on Stackoverflow
Solution 1 - GitmanojldsView Answer on Stackoverflow
Solution 2 - GitAli Reza DehdarView Answer on Stackoverflow
Solution 3 - GitGrant BirchmeierView Answer on Stackoverflow
Solution 4 - GitygoeView Answer on Stackoverflow
Solution 5 - GitSamView Answer on Stackoverflow
Solution 6 - GitTransformerView Answer on Stackoverflow