DEV Community

Cover image for Easy to create .gitignore for the dotnet developers
Rafal Pienkowski
Rafal Pienkowski

Posted on

Easy to create .gitignore for the dotnet developers

TL;DR Starting from .Net Core 3.0, you can take advantage of the build in the .Net framework gitignore file template just by typing dotnet new gitignore in your terminal.


I assume you are familiar with the idea of a gitignore file. In short words, a .gitignore file is a plain text file in which each line contains a pattern for files or directories to be ignored by the git.

I am a dotnet developer. I don't know if you had this problem, but every time I wanted to create a new repository on the git repository, I lacked the build-in .gitignore file. Russ Hammett made me realize that the GitHub platform has prepared a gititnore file. Interestingly it isn't named C#, CSharp, dotnet. Nope, none of those things. It's called VisualStudio. Quite obvious.Once again thanks Russ.

Repo creation

I like to have a tidy repository without /obj/* and /bin/* objects, or other IDE specific things included. Storing such files is both messy and could be dangerous. For instance, you could forget to clean from those artifacts your super-secret password.

One of the possible solutions was to copy a .gitignore file from one repository to another. Not a very elegant but effective solution. Another option is to use sites like https://www.gitignore.io/.

Today that has changed. The .Net Core 3.0 SDK gives us a hand. I bet you now dotnet new command. It has a lot of parameters like mvc, webapp, or webapi. Starting from SDK 3.0, there is a new parameter gitignore, which creates a dotnet gitignore file.

dotnet new gitignore
Enter fullscreen mode Exit fullscreen mode

The created file is a good starting point. It contains rules for the most popular IDEs like Visual Studio, Rider, Visual Studio Code, and tools like NCrunch, R#, Visual Studio profiler, or StyleCop. Of course, you can add your own rules to it.

There is still missing documentation on the docs Microsoft site, but you can check it on yourself by typing in your command prompt:

dotnet new
Enter fullscreen mode Exit fullscreen mode

and going through the documentation which is attached there. Here is

terminal output

I think that the online documentation will be completed soon. Meanwhile, you can take advantage of the build-in gitignore feature, and you don't have to copy the file from other repositories.

P.S. Don't forget about other dotnet cli commands and options.

Cheers!

Top comments (8)

Collapse
 
bramburn profile image
Bhavesh Ramburn

thanks was just looking for that!

Collapse
 
kritner profile image
Russ Hammett

Pretty sure there's a visual studio git ignore on that list from github, should get you most of the way there anyway. Cool stuff otherwise, wonder if the cli could even be pulling from github for the file

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

You're right that there is the VisualStudio gitignore option. I didn't think that someone can name named the gitignore file like IDE's name (not the platform/language) 😉

Collapse
 
kritner profile image
Russ Hammett

Yeah, it's a bit odd to be sure

Thread Thread
 
kandarpgautam profile image
kandarpgautam

It's not odd. but very obvious thing to do. there are different IDEs which creates different files that might not be necessary for the project but for the IDE.

Collapse
 
dyagzy profile image
dyagzy

Wow, thank you for this insightf. I often use the gitignore file for C++ from the github repository whenever I need to create one for my project. Now I have learnt a better way to do it.

Collapse
 
votanlean profile image
Alex Vo

Easy and perfect!

Collapse
 
ibrahimmf738 profile image
Ibrahim Mohamed Farah

Thanks, its great feature