Git: How exactly Ignore Works

Bruce Wen
5 min readMar 6, 2023

Most developers know that the .gitignore file under the root of a source repository can be used to ignore files that aren’t needed. But how exactly does gitignore work? What are the gitignore patterns? Is the .gitignore file the only place where gitignore patterns could be placed? These questions are addressed in this article.

Story Cover — Designed by Bruce Wen

gitignore Ignores Untracked Files Only

Man page says of gitignore has a short description about it:

gitignore — Specifies intentionally untracked files to ignore

In this sentence, the keyword is untracked.

That means if some files have already been tracked, then they cannot be ignored anymore — we have to remove them from index if don’t want to track them anymore. That might cause a bit confusions for people — why gitignore doesn’t work for me?!! Please check if those files are untracked.

When we get into the habit of saying “gitignore ignores untracked files only”, you will never have that confusion.

📓 Command to remove from index

git rm --cached [filename] or git rm -r --cached [folder name]

gitignore File Introduction

A gitignore file specifies intentionally untracked files that Git should…

--

--