Easy to create .gitignore for the dotnet developers
Rafal Pienkowski

Rafal Pienkowski @rafalpienkowski

About: I'm focused on developing and expanding my knowledge and skills. Enjoying new challenges. I'm assuming that there are no stupid questions, there are only silly answers.

Location:
Bydgoszcz
Joined:
Oct 24, 2017

Easy to create .gitignore for the dotnet developers

Publish Date: Dec 1 '19
63 8

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!

Comments 8 total

  • Russ Hammett
    Russ HammettDec 1, 2019

    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

    • Rafal Pienkowski
      Rafal PienkowskiDec 1, 2019

      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) 😉

      • Russ Hammett
        Russ HammettDec 1, 2019

        Yeah, it's a bit odd to be sure

        • kandarpgautam
          kandarpgautamDec 17, 2021

          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.

  • dyagzy
    dyagzyFeb 14, 2021

    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.

  • Bhavesh Ramburn
    Bhavesh RamburnFeb 1, 2022

    thanks was just looking for that!

  • Ibrahim Mohamed Farah
    Ibrahim Mohamed FarahMar 23, 2022

    Thanks, its great feature

  • An Vo
    An VoNov 17, 2023

    Easy and perfect!

Add comment