Git rm --cached: Stop Tracking a File and Keep It
Use git rm --cached to keep a local file while removing it from Git tracking. Verify the ignore rule and see what happens when another worktree merges.

Use git rm --cached -- path to stop tracking a file while keeping its current local copy. Add an ignore rule for the path, then check that the staged changes contain only the intended removal and the rule. When another worktree later merges that removal, Git can delete its clean tracked copy. Back up any local settings that teammates still need before they update.
Start by asking Git two questions. Is the path in this worktree’s index? Which rule would ignore it if it were untracked? Those answers tell you whether to edit a pattern or stage a deletion. In a disposable repository on Git 2.53.0, we used a dummy environment file and checked what happened in two linked worktrees before and after integrating the removal.
Check whether the path is tracked
Run these commands in the checkout you intend to change. This example uses a root-level .env containing disposable settings.
git status --short
git ls-files -- .env
git check-ignore -v --no-index -- .env
If git ls-files prints .env, the path is in the index. If git check-ignore -v --no-index prints a rule, the rule matches the path. Both outputs can appear at the same time. This is the common case where an agent added a rule to .gitignore and the next diff still includes changes to the file. The index already has an entry to compare against the working copy. Adding an ignore rule does not change that entry. Git ignore documentation
Ordinary git check-ignore -v -- .env skips tracked files. Add --no-index while diagnosing a tracked path so you can see the rule that would apply once the path leaves the index. The verbose output identifies the source file, the line number and the pattern. If the path is untracked and you get no match, check the pattern and the directory containing the ignore file. For example, /.env in the root .gitignore matches the root file. It leaves a nested examples/.env for you to handle separately. Git check-ignore documentation
For a repository-wide inventory, git ls-files -ci --exclude-standard lists tracked paths that match standard ignore rules. The output contains filenames. Review that list one path at a time with the repository owner. A tracked test fixture in a broadly ignored directory may be intentional. Use this list to open a review, and remove only paths with a confirmed reason to leave version control. Git ls-files documentation
| What you observe | What to do |
|---|---|
| The path is untracked and no rule matches | Add or correct a narrow ignore rule, then check the match. |
| The path is untracked and a rule matches | Leave the file local. Review the ignore rule if it is new. |
| The path is an established tracked file and belongs only on disk | Stage its index removal and the narrow ignore rule. |
| A tracked file has pending changes you only want to unstage | Use git restore --staged -- path to preserve tracking. |
| A tracked file is meant to remain a shared template or test fixture | Keep its index entry and reconsider the pattern or exception. |
Stage the smallest change that fixes the problem
Edit the root .gitignore and add /.env on its own line. If your project needs a shared setup example, keep a separate .env.example with placeholders suitable for the repository. Write those placeholders directly. Verify each value before staging that template. The developer onboarding checklist gives teams a place to document how new contributors provision their own local settings.
Preview the index removal before applying it. Use -- before the filename to separate the command options from the path.
git rm --dry-run --cached -- .env
git rm --cached -- .env
git add -- .gitignore
git diff --cached --name-status
The first command reports what would be removed. The second removes the index entry in this worktree. The third stages the ignore file you edited. In the fourth command’s output, expect D for .env and A or M for .gitignore, depending on whether the ignore file already existed. If the staged output includes unrelated files, pause and review the full staged file list with whoever owns those changes. The desired review is small enough that another developer can identify the purpose of each staged path. Git rm documentation
Git can refuse the index removal when the staged content matches neither the working copy nor the tip of the branch. If the dry run or the real command refuses, check the staged state and the working state separately. Decide which edits need preserving before changing the index. For a path that may contain credentials, use name-only or name-status output when sharing evidence. Avoid pasting a diff containing the file’s values into a pull request or agent conversation.
Verify the result in the same checkout before you commit. Confirm the local file still exists with your shell or file browser. Then check that the path is gone from the index and that ordinary ignore matching now reports the rule.
git ls-files -- .env
git check-ignore -v -- .env
git diff --cached --name-status
An empty successful git ls-files result is expected for this specific path. The ignore check should point at your intended rule, and the staged inventory should contain the reviewed deletion. Commit the removal and the rule together when those checks agree. The resulting commit records the removal from the repository’s next snapshot. The current local file stays available in the checkout where you ran the cached removal.
Treat generated files and shared configuration differently
Apply the same check to build output if it ends up in a pull request. In the fixture, build/artifact.txt was new and untracked. With /build/ in the root ignore file, Git reported the directory as ignored and left it off the staged list. The modified .env appeared next to it in the same status output because it was already tracked. Inspect both states before assuming an ignore rule failed.
M .env
?? .gitignore
!! build/
If a generated directory contains tracked project inputs alongside disposable output, check its tracked entries before removing a directory from the index. Build inputs and configuration should remain available to new contributors and CI. A narrow rule for the generated path is easier to verify. When a pattern is just for your own tools and you have no index removal to share, Git’s local exclude file can keep that rule in your checkout’s repository metadata. Keep rules that define what all contributors should commit in the project’s .gitignore. Git ignore documentation
For an agent handoff, write the expected end state in the task itself. For example, “The output under build/ stays local. The checked-in build script and the configuration template stay tracked. The PR stages the ignore rule and a deletion for .env.” A reviewer can compare those three statements to one staged name-status listing. That also gives a second agent a concrete way to notice if a broad cleanup proposal expands past the agreed paths.
Plan how other worktrees will receive the change
Each linked worktree has its own index and working copy. In our test, one checkout removed the dummy .env from its index and kept the edited local file. The second checkout stayed on its original branch and continued to track its clean copy. When it fast-forwarded to the first checkout’s commit, Git removed the clean tracked copy in the second checkout. The original checkout still had its ignored copy. The 16-check fixture used no remote and included no credentials.
| Point in the test | Worktree that staged the removal | Other worktree |
|---|---|---|
| Before the removal | File tracked with a dummy local edit | Original dummy file tracked and clean |
| After the first worktree committed | Edited file present locally and ignored | Original file still tracked and present |
| After the second worktree integrated that commit | Edited file still present locally | Clean tracked file removed by the update |
If you have a local service running from another worktree, tell its owner which file is being removed and how to recreate it. Ask each owner to preserve their current local settings through the team’s approved secret storage before merging. Coordinate parallel jobs that still expect the file on disk. If you need a clean place to verify what a fresh checkout receives, use the isolation steps in Git Worktree Add. A tracked placeholder and ignored per-worktree configuration make that test easier to repeat.
When an agent proposes the change, ask for a review note containing the exact path, the index check, the ignore-rule match and the intended effect on other worktrees. The note can say what fell out of the index without quoting the file. Treat a second worktree’s configuration as part of the handoff. A green test run in the worktree that kept its old local file is compatible with another checkout losing its tracked copy. Agent orchestration for coding teams
Handle a credential separately from the index change
A later deletion commit leaves earlier snapshots reachable. Our fixture verified that the older commit still contained its dummy file. If a real credential entered shared history, revoke or rotate it promptly through the provider, then follow your incident process for coordinating cleanup with repository owners. Review the credential’s scope and where it was used. Rewriting a repository is a separate project involving collaborators, references and copies of the old history. GitHub guidance on removing sensitive data
An ignore rule also applies to Git’s path handling. It cannot decide which application or CI job should be able to read a value. If the file belonged to a build or test workflow, make the required settings explicit and give that workflow only the credentials it needs. Keep test placeholders harmless. GitHub Actions permissions guide
Frequently asked questions
Does git rm --cached delete my local file?
It removes the named path from the index and leaves the working file in the checkout where you run it. If the path was tracked in the last commit, Git stages its deletion. Teammates who later integrate that deletion may lose their clean tracked copy, so coordinate local setup first.
Why does .gitignore still show a tracked file as modified?
Git continues to compare the working file with its index entry. The ignore rule applies to untracked paths. Check the entry with git ls-files and verify the matching rule with git check-ignore using its no-index option. Remove the entry only if the file belongs outside version control.
What is the difference between git restore --staged and git rm --cached?
Use git restore with the staged option to restore the index entry from the branch tip and unstage pending edits to a tracked path. Use git rm with the cached option when you want the next commit to remove that path from tracking while keeping this checkout’s working file.
How can I find tracked files that match .gitignore rules?
Run git ls-files -ci --exclude-standard to list tracked paths that match standard ignore rules. Then check each path individually with the verbose no-index form of git check-ignore. Review why each path is tracked before removing anything. A tracked fixture or template can be intentional even when a broad pattern matches it.
Does git rm --cached remove a committed credential from Git history?
The command can stage a deletion for a future commit. Earlier commits retain their recorded contents while they are reachable. If a real credential was shared, revoke or rotate it promptly and follow your repository incident process for reviewing exposure, existing copies and any coordinated history cleanup.