Stop tracking a file that is already in .gitignore

Remove a tracked file from the index while retaining your copy, commit the ignore rule, and avoid accidentally staging unrelated work during bulk cleanup.

Best practices
Illustration of Git branches and version history

Adding a tracked file to .gitignore does not stop Git from tracking it. Use git rm --cached -- path to remove its index entry while keeping the working-tree file, then commit that removal with the ignore rule.

Remove one file or directory from tracking

printf '%s\n' 'config.local.json' >> .gitignore
git rm --cached -- config.local.json
git add .gitignore
git diff --cached
git commit -m "Stop tracking local configuration"

For a directory, use git rm -r --cached -- build/ and a matching ignore rule. Check whether the rule already exists before appending another copy. After removal from the index, the ignore rule applies locally immediately; the commit records the new repository state for others.

The commands do not erase older versions from history. If the file contained a credential, rotate it and handle historical removal as a separate coordinated task.

Rebuild the index only when that scope is intended

From the repository root, a commonly used bulk sequence is:

git rm -r --cached .
git add .
git diff --cached --stat
git diff --cached
git commit -m "Align tracked files with ignore rules"

It removes and rebuilds the index, which can stage unrelated modifications, additions, and deletions. Start with saved work and a clear staging area, then inspect the full staged result. For a few files, targeted cached removals are easier to review.

Tell collaborators what will change

Your cached removal preserves your copy. When another checkout receives the deletion commit, Git can remove its tracked copy as part of the update. Local edits may cause Git to stop rather than overwrite them, but do not rely on that as a configuration backup. Preserve needed local files before applying the migration.

FAQ

Does --cached delete the disk file?

No. It changes the index. Without --cached, git rm also removes the working-tree copy. Git may refuse a cached removal if staged contents differ from both the committed and working-tree versions; inspect that state rather than adding force automatically.

Can I hide changes to a tracked configuration instead?

Git does not provide a reliable ignore mechanism for arbitrary edits to tracked files. skip-worktree is primarily for sparse checkout, and assume-unchanged is a performance hint. Prefer a tracked template plus an ignored local override or environment variables.

Do I need to push?

Commit first, then publish through the repository's normal workflow when the team is ready for the tracking change. The local operation itself does not require a remote.

See git-rm, gitignore, and Git's tracked-file FAQ.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting