Skip to article

How to Stop Tracking a File Without Deleting It

Keep a local file, remove it from Git tracking, and understand why .gitignore alone is not enough. Try the commands in a disposable browser VM.

A local settings file slipped into a commit. Now every change to your preferred theme appears in git status, even after you add the filename to .gitignore.

Add the file to .gitignore, remove it from Git's index with git rm --cached, then commit the change. Start with the first runnable step below. Each section has a Try it / Current image block with the commands and a Run all steps button. In your own repository, run the equivalent commands from the repository root, replacing the example filename with yours.

Review the staged changes before committing. The deletion shown there is intentional: the next commit will omit the file. Your local copy stays on disk because you used --cached. Running git rm without that option also removes the working file. The final -- separates options from the filename. See Git's git-rm reference for the command's behavior.

The example below uses harmless theme settings and a sample cache directory in a disposable browser VM. Follow the four steps in order; Reset terminal starts the example again.

01

See why .gitignore does not hide tracked changes

Why .gitignore alone does not work

The repository starts with local.settings.json already committed. Add its name to .gitignore, then change the theme from light to dark.

git status --short still shows M local.settings.json. The question marks beside .gitignore mean that the new ignore file is itself untracked. git ls-files confirms that the settings file remains tracked.

Git's ignore rules apply to untracked files. They do not stop Git from noticing edits to a tracked file. The leading slash in /local.settings.json anchors this rule to the directory containing .gitignore, which is the repository root here.

↳ TRY IT / CURRENT IMAGELive Git
cd /home/student/untrack-lab
printf '%s\n' '/local.settings.json' >> .gitignore
printf '%s\n' '{"theme":"dark"}' > local.settings.json
git status --short
git ls-files -- local.settings.json

Expected: status lists local.settings.json as modified and .gitignore as untracked. ls-files still lists local.settings.json.

 

02

Remove the file from the index while keeping the local copy

Remove the tracked entry, keep the file

The index is Git's staging area: it describes the next commit. git rm --cached -- local.settings.json removes the settings file from that area. Then git add .gitignore stages the ignore rule.

The staged summary should contain A for .gitignore and D for local.settings.json. Read the file with cat: your dark theme setting is still there. You have prepared a commit that stops sharing the file while preserving your local copy.

If Git refuses the removal because the staged content differs from both the last commit and your working copy, inspect git diff and git diff --cached and preserve any needed version before retrying. Avoid adding -f just to silence the check.

↳ TRY IT / CURRENT IMAGELive Git
cd /home/student/untrack-lab
git rm --cached -- local.settings.json
git add .gitignore
git diff --cached --name-status
cat local.settings.json

Expected: The staged changes add .gitignore and delete local.settings.json. cat still prints the dark theme settings.

 

03

Commit the removal and verify future edits are ignored

Commit and check another edit

Commit the staged changes, then change the theme once more. git status --short now prints nothing. The settings file is untracked and ignored, so this edit is no longer offered for the next commit.

git check-ignore -v shows the ignore file, line number, and matching rule. cat confirms that the solarized setting exists locally. Together, those checks explain the clean status: your file remains on disk, and the ignore rule is working.

This commit also removes the file for other checkouts. When teammates pull the removal into a clean checkout, Git deletes their previously tracked copy. Tell them to back up any needed settings outside the repository first, then restore their local file afterward. A fresh clone will not contain it either. For shared projects, keep a separate tracked example such as local.settings.example.json with safe defaults and setup instructions.

↳ TRY IT / CURRENT IMAGELive Git
cd /home/student/untrack-lab
git commit -m 'Stop tracking local settings'
printf '%s\n' '{"theme":"solarized"}' > local.settings.json
git status --short
git check-ignore -v -- local.settings.json
cat local.settings.json

Expected: The commit records the removal. status prints nothing, check-ignore identifies the .gitignore rule, and cat prints the solarized theme settings.

 

04

Stop tracking a whole directory

What about a whole directory?

The practice repository also contains a tracked local-cache/preview.txt. Add /local-cache/ to .gitignore and use git rm -r --cached -- local-cache/ to remove the directory's tracked contents from the index. The -r option makes the removal recursive; --cached keeps the local files.

Review the staged summary before the commit: .gitignore is modified, and local-cache/preview.txt is marked for removal. After committing, check the ignore rule and read the file. Its contents should still be Cached preview, while git status --short prints nothing.

The same teammate precautions apply to directories: the commit removes their tracked contents from other checkouts too. Preserve any needed local files before pulling that change.

↳ TRY IT / CURRENT IMAGELive Git
cd /home/student/untrack-lab
printf '%s\n' '/local-cache/' >> .gitignore
git rm -r --cached -- local-cache/
git add .gitignore
git diff --cached --name-status
git commit -m 'Stop tracking local cache'
git check-ignore -v -- local-cache/preview.txt
cat local-cache/preview.txt
git status --short

Expected: The staged summary removes local-cache/preview.txt and modifies .gitignore. After the commit, check-ignore identifies the directory rule, cat prints Cached preview, and status prints nothing.

 

Keep local files out of future commits

Untracking changes future snapshots. Earlier commits still contain the file. If it held a password or token, revoke or rotate that credential; adding an ignore rule and committing a removal does not erase the exposed value from history.

For an ordinary local settings file, the routine is small: ignore the path, remove it from the index with --cached, review, and commit. After that, keep editing your local copy as usual.