Sub topic 1
Tracked, Untracked, and Ignored Files
Prerequisites
This chapter assumes you can run git init, git add, git commit, and git status in a terminal. It also assumes you know that a repository's working tree is the set of files and directories you see on disk next to the .git directory.
The three states of a file
Git sorts every file in the working tree into one of three groups. Knowing which group a file is in explains most .gitignore behavior.
- Tracked: Git already knows the file. It is in the last commit or in the index (the staging area). Git reports changes to it.
- Untracked: Git has never been told about the file. It is not in the last commit or in the index.
git statuslists it and offers to add it. - Ignored: the file is untracked, and a rule says Git should leave it alone.
git statusdoes not list it, andgit add .skips it.
Ignored is not a fourth kind of file on its own. It is a label that applies only to untracked files. A tracked file is never ignored, even if a rule matches its name. This detail causes the most confusion later, so keep it in mind.
Seeing the states in practice
Create a small repository and watch a file move between states.
git init demo
cd demo
echo "hello" > notes.txt
echo "debug output" > app.log
git status --shortBoth files show up as untracked:
?? app.log
?? notes.txtNow commit notes.txt so it becomes tracked:
git add notes.txt
git commit -m "Add notes"
git status --shortOnly app.log is still listed as untracked. Next, tell Git to ignore log files:
echo "*.log" > .gitignore
git status --shortThe output is now:
?? .gitignoreapp.log has disappeared from the list because it is ignored. The .gitignore file itself is untracked. You normally commit it so that everyone who clones the repository shares the same rules.
How the states change
The arrow from Ignored to Tracked is missing on purpose. Plain git add refuses to stage an ignored file. You can only force it with git add -f, which a later chapter covers.
Why ignore files at all
Ignored files are usually ones that should not be shared or that can be rebuilt:
- Generated output, such as compiled code and build directories.
- Downloaded dependencies, such as
node_modules/. - Logs and temporary files.
- Local settings and secrets, such as
.envfiles. - Clutter from your editor or operating system.
Keeping these out of git status makes real changes easier to see. It also stops you from committing them by accident with git add ..
Common mistakes at this stage
- Expecting a rule to untrack a file. If a file is already tracked, adding it to
.gitignorechanges nothing. Git keeps reporting its changes. The fix is a later chapter. - Forgetting that ignoring is not deleting. An ignored file stays on disk. The rule only affects what Git reports and stages.
- Treating ignore rules as security. A rule prevents accidental staging. It does not remove a secret that was already committed, and it does not hide anything from someone with access to your disk.
Check your understanding
Exercise
You commit a file named settings.json, and later add settings.json to .gitignore. You then edit the file. Will git status show the edit, and why?
Show answer
Yes. The file is tracked, because it is already in the last commit. Ignore rules apply only to untracked files, so Git keeps reporting changes to it. To stop tracking it, you must remove it from the index, for example with git rm --cached settings.json.
Exercise
In a fresh repository you create build/out.js and a .gitignore containing the single line build/. Which of the three states is build/out.js in, and does git add . stage it?
Show answer
It is ignored. It was never tracked, and the build/ rule matches its directory. git add . skips it, so it is not staged.