Git
version control
.gitignore
file management
repository cleanup

How to remove files that are listed in the .gitignore but still on the repository?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

.gitignore only affects untracked files. If a file was already committed, adding it to .gitignore does not remove it from the repository automatically. To stop tracking it, you must remove it from the Git index while usually keeping the local copy on disk.

Why .gitignore Is Not Enough

Suppose a file such as .env was committed earlier, and later you add this rule:

gitignore
.env

Git still tracks the file because tracking state already exists in the index. .gitignore prevents future untracked additions; it does not rewrite history and it does not silently untrack existing files.

That is why repository cleanup requires an explicit Git command.

Remove the File from the Index but Keep It Locally

The standard fix is git rm --cached.

bash
git rm --cached .env

This means:

  • remove the file from version control
  • keep the file on your local filesystem

Then commit the change:

bash
git commit -m "Stop tracking .env"

After that, the .gitignore rule prevents Git from re-adding it accidentally.

Remove Many Ignored Files

If several tracked files are now supposed to be ignored, remove them from the index as a group.

One explicit pattern-based example:

bash
git rm --cached build/output.log
git rm --cached config/local.settings.json

For broader cleanup, many teams use:

bash
git rm -r --cached .
git add .
git commit -m "Refresh tracked files to respect .gitignore"

This works because:

  1. all tracked paths are removed from the index
  2. git add . re-adds only files not ignored by current rules

It is powerful, so review the staged changes carefully before committing.

Verify What Will Be Affected

Before changing anything, inspect ignored tracked files or the current ignore behavior.

Useful checks:

bash
git check-ignore -v .env

and:

bash
git ls-files

If you are unsure whether a file is tracked, git ls-files path/to/file is a simple confirmation.

For repository cleanup, review git status and git diff --cached before committing.

Directory Example

If an entire directory is now ignored:

gitignore
dist/

and it was previously committed, untrack it with:

bash
git rm -r --cached dist
git commit -m "Stop tracking build output"

That keeps the local directory while removing it from the repository in the next commit.

This Does Not Rewrite History

Stopping tracking in the current commit does not remove the file from past commits. If the problem is just noisy generated files, that is usually enough.

If the file contains secrets, the problem is different. In that case, you may need to:

  • rotate the exposed secrets immediately
  • rewrite Git history with a history-rewriting tool
  • force-push if your team agrees on the cleanup process

That is a separate and more serious operation than ordinary .gitignore cleanup.

Coordinate with Teammates

When tracked files become ignored, teammates may still have old copies or stale branch state. After the cleanup commit lands, collaborators usually just pull normally, but they should understand why those files disappeared from version control.

For environment-specific files, commit a safe template if needed:

gitignore
.env
!.env.example

That preserves onboarding clarity while keeping secrets or local machine config out of the repo.

Common Pitfalls

  • Assuming .gitignore automatically removes already tracked files.
  • Using git rm without --cached and accidentally deleting local files too.
  • Running large cleanup commands without reviewing the staged diff.
  • Treating secret exposure as a normal ignore cleanup instead of a security incident.
  • Forgetting to keep an example template file for configuration-driven projects.

Summary

  • '.gitignore prevents tracking of new untracked files, but it does not untrack committed files.'
  • Use git rm --cached to remove a file from the repository while keeping it locally.
  • For wider cleanup, untrack the index and re-add files that still belong under version control.
  • Review staged changes carefully before committing repository cleanup.
  • If sensitive data was committed, cleanup may require history rewriting and secret rotation, not just .gitignore.

Course illustration
Course illustration

All Rights Reserved.