Add Git Ignore to an existing Visual Studio Project

Most Development nowadays is already using GIT repositories. One of the common problems that may encounter using this method is when GIT tracks a not needed file. One of the ways to prevent this kind of situation from happening is to use the .gitignore file. This file contains all the files that are being ignored and excluded from tracking. In this article, I will walk through adding Ignore file to an existing Visual Studio Project and then show you how to stop Git from tracking newly added files from ignoring files.

Add a .gitignore to an existing Visual Studio Project

Let’s start by opening a Visual Studio project once you’ve done that, open Team Explorer.

Team Explorer

Then click on the home icon to navigate to Home menu and click the settings option

Home Menu

.Then click Repository Settings.

Repository settings

If you already added an ignore file yet, Edit button will be displayed instead of Add.

Add Git Ignore to an existing Visual Studio Project

Now click on the Add link from the Ignore File option. This option will add a .gtiignore file with default sets of files excluded for tracking. This file is base on the template for Visual Studio projects. You could also add the file manually. You can refer to this GitHub repo. This repository contains templates for .ignore file for Visual Studio Project.

You can edit the file if you want to add specific file to exclude from tracking changes.

Add Git Ignore to an existing Visual Studio Project

This step is how you can Add Git Ignore to an existing Visual Studio Project. Sometimes adding a .ignore file may not work as expected, especially if you have added your project to a Git Repository without adding the Ignore file. To fix that, you may follow the steps.

Untrack files already added to git repository based on the .gitignore

To stop tracking the files in the ignore the file, open a command prompt, navigate the directory containing your solution file (.sln), and run the following commands.

Note: Make sure you have Git Installed on your machine.

Git Command Prompt

1. Commit all your changes

Make sure that all your changes are committed, including your .gitignore file.

2. Removes all the file from the index

To do that, use the command below. This command will remove all your file from the index but leaves it in the working directory.

git rm -r –cached .
  • rm this command will remove all or specific files.
  • -r will allow recursive removal
  • –cached will only remove files from the index. But retain your file from your physical file directory.
  • The . indicates that include all files will be untracked. You can also untrack a specific file with git rm --cached files.txt.

The rm command will untrack all your files from your project directory. If you wish to try what it does beforehand, add the -n or --dry-run flag to test things out.

3. Re-add Everything

git add .

4. Commit all Files

git commit -m “.gitignore”

To avoid this situation on your next projects. Make sure you have added Git ignore file before proceeding to push your file to a Git repository. Hopes this helps. Happy Coding!

Please also do check this Article.

Leave a Comment

%d bloggers like this: