Should .csproj files be added to .gitignore?

Viewed 2853

I'm wondering if adding .csproj files in the git history is really useful / best / bad practice

(Currently, I'm using git for private repositories)

Thanks!

2 Answers

Short answer: You definitely need .csproj files in your git history.

Long answer: Here's how to answer a question like that. Make a copy of your solution folder. Delete any files you wonder if they should be included in the git history. Try to build and run your code. If you can't, that means they are needed in git, and vice-versa.

In this case, you would discover that your project will not build without the .csproj file, hence that file must be in git.

It's also important to remove from git files that are not needed to build and run the project, such as files that are generated as part of the build process.

For the most part, this work has been done for you already, for example https://github.com/dotnet/core/blob/master/.gitignore has a list of directories and extensions that should be ignored for .Net Core projects. Other platforms/languages have similar .gitignore files posted.

In addition to the answer above, it helps to know what project files are:

When you create and build solutions in Visual Studio, Visual Studio uses MSBuild to build each project in your solution. Every Visual Studio project includes an MSBuild project file, with a file extension that reflects the type of project—for example, a C# project (.csproj), a Visual Basic.NET project (.vbproj), or a database project (.dbproj). In order to build a project, MSBuild must process the project file associated with the project. The project file is an XML document that contains all the information and instructions that MSBuild needs in order to build your project, like the content to include, the platform requirements, versioning information, web server or database server settings, and the tasks that must be performed.

Hence the reason why (emphasis mine):

In this case, you would discover that your project will not build without the .csproj file...

Hth...

Related