Click here to Skip to main content
15,860,844 members
Articles / Operating Systems / Windows

Keep Nuget Packages Out of Source Control with Nuget Package Manager Restore

Rate me:
Please Sign up or sign in to vote.
4.84/5 (14 votes)
21 Nov 2013CPOL4 min read 50.5K   19   9
Keep Nuget packages out of source control with Nuget Package Manager Restore.

NuGet_project_logo.svgNuget is cool.

What is not cool is when you push a project to source control and include a ton of installed packages, bulking up your repo with stuff that could be easily pulled in via Nuget by anyone cloning your project. However, what was equally irritating was having to include instructions for what packages need to be installed, if they were excluded from the source repo.

Enter Nuget Package Restore.

 

Allow Visual Studio to Restore Nuget Packages During Build

Before you can avail yourself of Nuget Package Restore, we need to explicitly allow this behavior on our machine. This is required on a per-machine basis before Nuget can restore packages for a solution automagically, and can be set in Tools => Options => Package Manager:

Visual Studio Options Menu:

vs-tools-options-menu

Package Manager Settings Dialog:

package-manager-settings

Now that our machine has been told it is ok to restore Nuget Packages automatically, we need to enable this behavior in our specific solution as well.

Enable Nuget Package Restore for the Solution

To enable Nuget Package Restore in our specific solution, right-click on Solution Explorer => Your Solution => Enable Package Restore:

Enabling Nuget Package Restore for a Specific VS Solution 

enable-package-restore-in-solution

This may take a few seconds. When VS is done processing, an examination of Solution Explorer reveals a new folder at the root solution level:

Files Added to Solution by Nuget Package Restorenuget-package-restore-files-added

 

Notice the NuGet.exe file in there? This file is required when, for example, someone clones your project from source and attempts to build with Nuget Package Restore enabled. Therefore, it needs to be committed to source control.

We also now want to exclude all the package files from source by editing our .gitignore file (I am assuming Git as the source control provider, but the principle here applies to whichever provider you are using – you need to tell your version control system what to include in the repository and what to keep out).

Edit .gitignore to Exclude Packages and Include NuGet.exe

A typical ASP.NET MVC 5 project created from the standard VS 2013 template will contain (as of this writing) 161 files from various Nuget packages included as part of the default solution. This is some significant bulk. Using the default .gitignore file (or any of the most common in use for Visual Studio/C# projects) the total number of files which will tend to be included as part of the project repo numbers over 200. Note this is after excluding all the binaries and VS housekeeping files. In other words, nearly 75% of the common files in a VS 2013 ASP.NET MVC 5 solution consist of Nuget packages, which can now be restored automatically instead of pushed up to our repo.

To cut down the bulk, lets modify our .gitignore as follows. Note, your .gitignore (or .hgignore, or .tfignore, what have you) will most likely look a little different than mine. That's okay, because we're only going to do a few small tweaks to the file – keep the rest the same.

Ignore Packages Folder

Open your .gitignore file and add the following line to exclude the packages folder from source:

Ignore Packages Folder in .gitignore 
packages*/

 

Make an Exception to Include NuGet.exe

Most likely, your .gitignore file already excludes files with the .exe extension. We may want to make an exception for Nuget.exe, since it will be needed to restore packages by anyone cloning our repo:

Look through your .gitignore until you find the following:

*.exe

 Then add this right AFTER it (the order here is important):

*.exe
!NuGet.exe

 The above tells Git to ignore .exe files, but to make an exception for NuGet.exe.

Another option (pointed out by a commentor on G+), if you don't want to clutter up your .gitignore, is to use the following command, which force-adds the (otherwise ignored) Nuget.exe file to the git staging for commit: 

$ git add -f Nuget.exe 

The step(s) above are optional, since someone cloning your solution could simply Enable Nuget Package Restore for the solution, at which point the NuGet.exe would be installed. However, this saves the step.

Inform Potential Consumers of Your Code

Now, we no longer need to include all the installed Nuget package files in our source repo. However, we should probably add a blurb to the README.txt file (or otherwise inform consumers of our code) letting users know that they will need to perform the VS configuration needed to allow Nuget to restore packages as described in the first step in this article.

Additional Resources and Items of Interest

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer XIV Solutions
United States United States
My name is John Atten, and my username on many of my online accounts is xivSolutions. I am Fascinated by all things technology and software development. I work mostly with C#, Javascript/Node.js, Various flavors of databases, and anything else I find interesting. I am always looking for new information, and value your feedback (especially where I got something wrong!)

Comments and Discussions

 
QuestionLinks in Table of Content are broken Pin
Michael Freidgeim23-May-16 22:08
Michael Freidgeim23-May-16 22:08 
AnswerRe: Links in Table of Content are broken Pin
John Atten24-May-16 1:43
John Atten24-May-16 1:43 
GeneralWorked like a charm for me. Pin
yash_agarwal12-Dec-14 0:34
professionalyash_agarwal12-Dec-14 0:34 
GeneralRe: Worked like a charm for me. Pin
John Atten12-Dec-14 1:03
John Atten12-Dec-14 1:03 
QuestionDO NOT DO THIS! Baby kittens will die! Pin
JeremyWilkins10-Dec-14 9:20
JeremyWilkins10-Dec-14 9:20 
AnswerRe: DO NOT DO THIS! Baby kittens will die! Pin
John Atten10-Dec-14 11:05
John Atten10-Dec-14 11:05 
SuggestionRe: DO NOT DO THIS! Baby kittens will die! Pin
sluki15-Jul-15 23:13
sluki15-Jul-15 23:13 
GeneralRe: DO NOT DO THIS! Baby kittens will die! Pin
John Atten16-Jul-15 0:50
John Atten16-Jul-15 0:50 
GeneralVisual Studio 2013 not ignoring packages directory Pin
HenningJe12-Nov-13 22:11
HenningJe12-Nov-13 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.