Last week I mentioned adding a .gitignore file to keep a configuration file from causing issues across machines. Visual Studio make it super easy to add, but the next time I made a change to the project the configuration file showed up in my changes again. Turns out that if Git is already tracking a file adding it to the ignore file will not do anything. I am going to walk through adding an ignore file and then cover the one of the processes that can be used to stop Git from tracking files that are in your ignore file.
Using Visual Studio to add a .gitignore file
Inside of Visual Studio open the Team Explorer window. If you don’t already have it open use the quick launch in the upper right hand side of the window to search for it. If it is not already on the Home page click the house icon in the top of the Team Explorer window.
Click the settings option.
Then click Repository Settings.
Now click the add link next to the Ignore File description. This will add the .gitignore file will all the defaults set for things that should be ignored. You could add the file manually, but then you would not get the nice set of default values. If you do decide to add the file manually this repo contains all the defaults that should be ignored for a project using .NET/Visual Studio.
Now that the file exists check it in.
Stop tracking files that should be ignored
To stop tracking the files in the ignore file open a command prompt and navigate to the directory that contains your solution file (.sln) and run the following commands.
git rm -r --cached . git add . git commit -am "Remove ignored files"
That seemed to do the trick for me. The git commands I found here. If you click on that link you will see there are lots of options on which commands to use for this process. If the above doesn’t work for you one of the other answers should meet your needs.
In the future I will be making sure my ignore file exists first just to avoid any issues.
20 thoughts on “Add Git Ignore to existing Visual Studio Project”
Really helpful thanks
Happy to hear it. Thank you for the feedback Steve!
It walks like a train (dutch for it works like a charm).!!!
Thank you for sharing this!
Great help !
Happy to hear it.
Thanks, it was really helpful and easy to follow.
that was really helpful , thank you
Thanks! it was really helpful
Great tidbit! I always seem to forget adding the .gitignore when setting up a project. Also, might want to add the “git push” command at the end of the commands.
Thank you so much !
Thanks, It is easy to follow
Awesome, dude. Just what I was looking for
Thank you, excellent “how-to”!
thank you soo much , it was exactly what i needed 🙂
After looking for a solution for hours. This finally was a working solution!