I’ve been building a .Net Core API recently and after getting the repo set up I realised there was a problem. The bin and obj folders had been pushed to the remote branch, so once our build was kicked off, it was failing. It transpires that there’s an issue with the Visual Studio git ignore file (specifically Visual Studio 2019). I’m not too sure of the exact details of the problem, but I can tell you how I fixed my dotnet gitignore folder problem.
Table of contents
Use the correct Visual Studio dotnet gitignore file
First of all make sure you are using the correct .Net core gitignore file. At the time of writing this was the one I used : https://raw.githubusercontent.com/github/gitignore/master/VisualStudio.gitignore
To see if you already have a .gitignore file you can go to Git > Settings, then make sure you have selected Source Control > Git Global Settings and you’ll see this:
If you don’t already have a gitignore file in your .Net core project then you need to create one in the root of your solution, in the same place as your .sln file. The file needs to be called ‘.gitignore’. You can then copy the contents from the link above, and save it. If you DO already have one, then just edit the contents, and save it. You can do that manually or you can click the Add button which will show instead of Edit in the above image if you don’t already have one.
Updated November 2022 : since writing this post I’ve written another one explaining how to add a gitignore file to your solution in Visual Studio 2022.
If you’ve already pushed your bin and obj folders to the remote repo
Even though you’ve fixed your .gitignore file, it won’t fix the fact that you’ve already pushed your bin and obj folders to the remote repo. So now we need to resolve that, which thankfully is easy. There are 2 steps:
- Delete both the bin and obj folders from your remote repo
- Delete both the bin and obj folders from your local repo
The problem is now fully resolved! However in addition to this I also found a .suo file had been pushed that shouldn’t have been, so I also had to delete that from both the remote and local repos. In my solution the file was here:
.vs/SolutionName/v16/.suo
You can now build your project and git will correctly ignore the bin and obj folders and the .suo file.
How to change .Net Core gitignore file to ignore the .vs folder
The same as above, delete the folder from the remote repo, then delete it from the local repo. I then had to do a git pull
and a git push
to resolve everything with git. (Incidentally if you want any help understanding git then try reading this post).
So that is how I fixed my ‘Visual Studio git ignore’ problem – I hope it helps you too!