Ignore existing files or folders in Git

Tech Knowledge
Published on October 26, 2024

Introduction

In Git, even if you add files or folders you want to ignore midway through to .gitignore, files that are already tracked will not be ignored.

Therefore, the following steps are required.
As an example, we will take the case of ignoring the obj folder later.

1Add the folder or files to ignore to .gitignore

# .gitignore
obj/

2Remove cache from Git's index

To ignore files that are already tracked by Git, remove them from the index.

git rm -r --cached obj/

Files in the obj folder will be removed from the index and will no longer be tracked.
*Local files will remain and not be deleted.

3Commit the changes

Commit the changes you removed from the index to Git.

git commit -m "Update .gitignore to ignore obj folder"

The procedure is now complete.