Git ignore rules and tracked files — a reproducible local experiment
Recorded for the Gumi article dated 2026-10-22.

FILES
- gitignore-index-lab.py: creates temporary repositories and checks eight cases.
- gitignore-index-lab-results.json: actual output, including 65 Git invocations.
- gitignore-index-lab-README.txt: this file.
All three are included at the root of gitignore-index-lab.zip.

RUN
Tested on macOS 27.0.1, arm64, CPython 3.12.14, Git 2.55.0.
Python standard library only; Git must be installed and available on PATH.
Only this environment was actually tested. Source syntax is Python 3.9+.

python3 gitignore-index-lab.py > result.json

The script creates repositories under an automatically removed temporary
directory. It does not use the current repository, access the network,
write global Git configuration, or access credentials. The clones' origin
is a local path to a disposable repository, not GitHub or another server.
All settings and file contents are invented demonstration values.

The script disables system/global Git config for its child processes and
sets per-command sample identity, disabled signing/hooks, and LF behavior.
The sample email lab@example.invalid is fictional. Commit dates are fixed
to the article date to help reproduce the recorded experiment. That date
is an experiment label, not a live timestamp added on every execution.
Temporary path prefixes in the trace are replaced with <lab>.
Other Git versions/platforms may produce different messages or identifiers;
environment fields and exact output bytes are not promised to match there.
The expected exit codes are checked explicitly, including intentional failures.

中文说明
问题：文件名已写进 .gitignore，为什么 git status 还显示修改？
先区分工作区、索引（暂存区）和当前提交。忽略规则不会移除已有索引条目。
实验用 local-settings.txt 的三份演示内容区分不同状态：
  第一版 mode=demo-v1；第二版 mode=demo-v2；第三版 mode=demo-v3。
不是实际应用配置，没有接入应用验证配置的读取、生成或迁移。
取消跟踪会改变后续提交，并不删除历史提交中的原内容。
--cached 只保证执行该命令时不删除本机的工作区文件，不能据此保证
其他克隆更新提交后也保留文件。干净更新与本地已有修改要分别检查。

日本語説明
問い：.gitignore に書いたファイルが、なぜ git status に変更として出るか。
作業ツリー、インデックス（ステージング領域）、現在のコミットを区別します。
無視ルールは既存のインデックス項目を取り除きません。
local-settings.txt の状態は、架空の内容で識別しています：
  第一版 mode=demo-v1、第二版 mode=demo-v2、第三版 mode=demo-v3。
実アプリの設定ではなく、読み込み、生成、移行の統合検証もしていません。
追跡停止は後続のコミットを変え、過去のコミットの内容は消しません。
--cached が実行時に自分の作業ツリーファイルを残すことと、別クローンが
コミットを取り込んだ際にも残ることは別です。クリーンな更新と、
ローカル変更のある更新を別々に確認しています。

EIGHT RECORDED CASES
1. tracked_file_with_matching_ignore
   Commit the file, commit its ignore rule, then edit the working file.
   git status --short: " M local-settings.txt\n".
   ls-files confirms the index entry. Default check-ignore -v exits 1 with
   empty stdout; --no-index reports .gitignore line 1. ls-files -ci with
   --exclude-standard lists the same file. A normal git add stages the edit,
   producing "M  local-settings.txt\n" despite the matching rule.

2. cached_removal_and_undo
   Restore the index from HEAD while preserving the modified working file.
   git rm --cached stages D, retaining mode=demo-v2 on disk.
   Before committing, git restore --staged restores the old index entry
   without changing that working content. Remove again and commit:
   normal status is empty, --ignored shows "!! local-settings.txt\n".
   git show HEAD~1:local-settings.txt still reads mode=demo-v1.

3. clean_clone_fast_forward
   A clean clone made before removal fetches and fast-forwards successfully.
   It receives .gitignore and applies the committed deletion: file absent.

4. modified_clone_fast_forward
   Another clone has mode=peer-local before updating. The update exits 1;
   its HEAD and local file content are unchanged. This is a refused update,
   not a completed update that preserves a now-untracked local file.

5. staged_but_never_committed
   A new file is added to an initialized repository, but never committed.
   Creating an uncommitted ignore file leaves status "A  local-settings.txt".
   git restore --staged removes the new index entry, preserves the disk file,
   and status --ignored shows !!. Ignore rules work locally before commit.

6. explicit_add_and_force
   For a genuinely untracked ignored file, explicit normal add exits 1
   without creating an index entry; add -f exits 0 and stages A.

7. divergent_index_cached_removal
   HEAD contains v1, index v2, working file v3. Non-forced rm --cached exits 1.
   Both the distinct index content v2 and working content v3 remain intact.

8. excluded_parent_negation
   cache/ followed by !cache/keep.txt still excludes the child via the parent.
   In this flat sample, cache/* followed by !cache/keep.txt makes keep.txt
   untracked (??), while discard.txt remains ignored (!!).

SCOPE
These are ordinary local repositories and fast-forward updates. No sparse
checkout, submodules, GUI clients, all merge-conflict cases, initial unborn
branch restore behavior, real-team migration or history rewrite was tested.
The fixed sample filenames avoid special pathspec and shell-expansion cases.
The parent-negation example contains only direct child files, not arbitrary
nested directories. The script's assertions concern these chosen cases.

PRIMARY REFERENCES (read 2026-10-22)
https://git-scm.com/docs/gitignore
https://git-scm.com/docs/git-status
https://git-scm.com/docs/git-ls-files
https://git-scm.com/docs/git-check-ignore
https://git-scm.com/docs/git-rm
https://git-scm.com/docs/git-diff
https://git-scm.com/docs/git-restore
https://git-scm.com/docs/git-add

The documentation states command semantics; the supplied JSON records the
independently executed local experiment and its expected negative results.
