Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I stop tracking a file in Git while keeping my local copy?

Quick answer

Run git rm --cached -- path/to/file. This removes the path from Git’s index while leaving the working-tree file in place; the removal is recorded for a later commit, not applied to earlier history. If you want Git to keep ignoring the local file, add a matching pattern to .gitignore: ignore rules do not untrack files that are already tracked. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a file in my repository that I want to keep on my computer but stop including in future commits. What command should I use?

Geez

Use git rm --cached -- path/to/file, replacing the example path with the path to your file. The --cached option removes the path from Git’s index but leaves the working-tree file untouched. [S1]

For example, if the tracked file is settings/local.json, run:

git rm --cached -- settings/local.json

Afterward, check that the file is still present in your local folder before committing. The index change is recorded for a later commit; it does not remove the file from earlier commits or rewrite history. [S1]

Follow-up question

If I add that file to .gitignore, can I skip git rm --cached?

Geez

No. An ignore rule by itself does not stop Git tracking a file that is already tracked. First remove the file from the index with git rm --cached; then add a suitable pattern to .gitignore if you want Git to ignore the local file afterward. [S2]

For the example above, you could add this line to .gitignore:

settings/local.json

That pattern is an example for that particular path, not a universal rule. Choose a pattern that matches only the file or files you intend to ignore. Keep the local file in place and check it remains present after running the removal command.

Follow-up question

How can I confirm I haven’t deleted my copy, and what should I expect before I commit?

Geez

Before committing, check the file directly in your working folder: open it or confirm that the path still exists. Then review the repository’s pending changes and make sure the intended file is the one being removed from the index. The --cached operation is specifically the index-only option; the working-tree file is left untouched. [S1]

If you added an ignore rule, check that .gitignore contains the intended pattern too. These are separate changes to review: the index removal records that Git should stop tracking the path, and the ignore rule helps prevent the local file from being reintroduced in later commits. [S2] If either change is unintended, pause and review before committing.

Follow-up question

Can you give me a small safe-to-adapt sequence, and clarify whether this removes the file from old commits?

Geez

Assuming the file is settings/local.json and you want that exact path ignored, adapt this sequence:

  1. Run git rm --cached -- settings/local.json.
  2. Confirm settings/local.json still exists in your working folder.
  3. Add settings/local.json to .gitignore.
  4. Review the pending changes, including the index removal and the ignore-file edit, before deciding whether to commit.

The removal is a change for a later commit; it does not erase the file from earlier commits or alter that history. [S1] These steps address future tracking, not removal of past copies. The documentation does not establish how to remove sensitive data from repository history, so don’t treat this procedure as a history-cleanup method.

A practical checklist

  • Replace the example path with the exact file path in your repository.

  • Run git rm --cached -- path/to/file to remove the path from the index only. [S1]

  • Confirm the local file is still present before committing.

  • Add a matching .gitignore pattern if you want the local file ignored afterward. [S2]

  • Review both the index removal and any ignore-file edit before committing.

A few more useful answers

Does adding a file to `.gitignore` untrack it automatically?

No. Ignore rules do not affect files already tracked. Remove the file from the index first, then add the ignore pattern if needed. [S2]

Will `git rm --cached` delete the file from earlier commits?

No. It records an index removal for a later commit and leaves earlier history unchanged. [S1]

What if I want to stop tracking the file but keep it eligible for Git again later?

Use the index-only removal and omit the ignore rule. Without a matching ignore rule, this procedure does not ask Git to ignore the local path afterward. [S1] [S2]

Sources

References used for this guide.

  1. [S1] Git rm documentation: remove a path from the index only
    git-scm.com · Checked
  2. [S2] Git ignore documentation: ignored paths and already-tracked files
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work