Using Git with Visual Studio C# and VB.NET projects

I just recently started using git for source control with some C++ projects and have been loving it! Naturally, I also wanted to start using it with some C# .NET projects in Visual Studio.

There are a number git add-ons for Visual Studio but the source control addon system doesn't lend itself to the distributed, pull-edit-merge, workflow of distributed SCMs. To work around this limitation most add-ons require you to use a secondary interface to add/remove files, commit, and push changes. Using the git shell directly turned out to be the most simple and effective interface.

.gitignore

When using the git shell, managing all of the intermediate and non-source files that an IDE and build system create can be a bit of a pain. Because git doesn't automatically use the IDE's solution and project to know which files should be placed under source control we need to provide it a blacklist of files to ignore. This practice is common with projects that are not managed by an IDE. When using git, the .gitignore file is what tells git which files it should ignore (I know, big shocker!).

Where to put it?

When you run a git command like git add, git will build a list of ignore rules starting at the root of the work tree and working down through the directory hierarchy to where the file that it is operating on is located. Conflicting rules further down the tree will override rules above them.

If your repository only contains your Visual Studio project then you can put these rules in the .git\info\exclude file, this is like a .gitignore that applies to the whole repository. However, if you have a number of heterogeneous projects dotted throughout your repo then just drop the .gitignore file in the same directory as your solution file.

What should I put in it?

I harvested the rules I'm using from a number of sources and aggregated them into this list. These rules will exclude build output and local, user or machine specific, files. If you have other rules that you've found useful please post them in the comments and I'll expand this list.

  
*.user           #User specific solution and project settings
*.suo
*.sln.cache
[bB]in/         #Build output
[oO]bj/
*.xap
_ReSharper.*  #Resharper database  
TestResults/   #MSTest result files  

Now the magic happens, you can simply run git add -A . from your solution directory any time that you add, change, or remove a file from your solution. Git will now only pay attention to files that you haven't blacklisted in your .gitignore. The git status command will also exclude these files from it's Untracked Files list; this makes finding what's changed in your local tree much easier.

Awesome!

This small bit of up-front work has made using git with Visual Studio much more productive for myself. If you've tried git in the past and found it to be a bit too much work, give this a try; I think you'll like it.