Using NuGet and Git Together

Just had to setup a new project with NuGet packages using Git as source control. In order to set this up properly you need to add

#Nuget
packages/

to your .gitignore file so that all the binaries, configs and what ever other resources the NuGet packages contain don’t get committed to your repository. This can significantly speed up push and pull requests between repositories with .NET code since you can exclude many megabytes of unnecessary synchronisation and rely instead on NuGet to restore the binary files when needed.

Package Restore Setting

Every developer needs to also set up their machine for package restore in Visual Studio: Tools menu -> Options -> Package Manager -> General : ensure that package restore during build is selected. If you have a build server then it needs to be setup with an environment variable EnableNuGetPackageRestore set to true to enable package restore. NuGet.org has a step by step guide to the setup.

When you commit you should include the .nuget folder in your repository, this is a bootstrapper used to download the required packages when you build.

Rewriting History

If you have already committed the packages folder, like I had, you may have an additional 30-50 MB of files in your repository… To purge these files you can run a filter-branch command to purge all references to you packages folder from the repository. Filter-branch rewrites you history so it is a dangerous command and you better make sure you use the correct syntax… In order for this to work as expected you need to follow the following steps:

  1. Get all developers to commit and push their work to the central repository
  2. Get all developers to take a break from working on code
  3. Run filter-branch on your clone of the repository
  4. Force push all branches from your clone to the central repository
  5. Get all developers to rebase their clones or to make it really easy just delete all clones and create new ones.

If you want to do  a step by step filter-branch and see the result on your repository size then Igor Zevaka has a good guide. The git commands below are based on several sources (including trial and error) but github has an additional explanation of the steps.

I am working on an MVC 4 project and following these steps the size of the local repo was reduced from 9 MB compressed to 9 KB.

The required commands for removing the packages folder from your history, including all branches & tags, are as follows – but remember you do this at your own risk – so test it out before you do it on a live repository…

echo compress repository
git gc
echo check size now for reference
git count-objects -v
echo remove packages folder from history
git filter-branch --index-filter "git rm -r --cached --ignore-unmatch packages/" --prune-empty --tag-name-filter cat -- --all
Echo compress and clean repository
rmdir /s .git\refs\original
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
echo check size after for reference
git count-objects -v
echo push back to origin
git push --all --force "origin"
echo now all other developers need to create new repository clones

This also includes pushing to your central repository but be aware that the central repository won’t immediately clean up loose and unreferenced objects. The default configuration of git is to leave dangling objects for two weeks before garbage collection removes them.

One Response to “Using NuGet and Git Together”

Read below or add a comment...

Trackbacks

  1. Setup NuGet on Visual Studio…

    Go to Package manager settings:   Add ISAT NuGet G…



Leave A Comment...

*