Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 21 Nov 2013
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

John Atten
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#, Java, SQL Server 2012, learning ASP.NET MVC, html 5/CSS/Javascript. I am always looking for new information, and value your feedback (especially where I got something wrong!)
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralVisual Studio 2013 not ignoring packages directory PinmemberMember 1002603712-Nov-13 22:11 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 21 Nov 2013
Article Copyright 2013 by John Atten
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid