Home » C# » .Net Core Visual Studio git ignore bin and obj folders

.Net Core Visual Studio git ignore bin and obj folders

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 problem.

Using the correct Visual Studio 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

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.

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:

  1. Delete both the bin and obj folders from your remote repo
  2. 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 git ignore 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!