Add Git Ignore to existing Repository

Most Development nowadays is already using GIT repositories. One of the common problems that may be encountered 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 files to the existing repository and then show you how to stop Git from tracking newly added files from ignoring files.

What is .gitignore?

A .gitignore file is used to specify which files and directories should be ignored (i.e., not tracked) by Git. This file is typically placed in the root directory of a Git repository, and it contains a list of file patterns or directory names that Git should exclude from version control.

For example, to ignore all files with the .log extension and the build/ directory, your .gitignore file might look like this:

# Ignore log files
*.log

# Ignore the build directory
build/

Add a .gitignore to an existing repository in 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 the Home menu and click the settings option

Home Menu

Then click Repository Settings.

Repository settings

If you already added an ignore file, the 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 based 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 the .gitignore file for the Visual Studio Project.

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

Add Git Ignore to an existing Visual Studio Project

This step is how you can Add Git Ignore to the existing repository in the 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 the 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 the Git ignore file before proceeding to push your file to a Git repository. Hopes this helps. Happy Coding!

For more tutorials like this, you can visit freecodespot.