Adding a file to `.gitignore` tells Git to ignore **untracked** files matching that pattern. But if the file was already committed, `.gitignore` has no effect on it. Git will keep tracking changes to that file until you explicitly remove it from the index.

## Remove a single file from tracking

Use `git rm --cached` to stop tracking a file without deleting it from your local filesystem:

```bash
$ git rm --cached config/database.yml
rm 'config/database.yml'
```

The file stays on disk, but Git treats it as untracked from this point forward. Your `.gitignore` rule will now apply to it.

## Remove a folder from tracking

For an entire directory, add the `-r` (recursive) flag:

```bash
$ git rm -r --cached logs/
rm 'logs/debug.log'
rm 'logs/error.log'
```

Everything inside `logs/` is removed from the index but remains on your machine.

## Remove all tracked-but-ignored files at once

If you have updated `.gitignore` with several new rules and want to clean everything up in one pass:

```bash
$ git rm -r --cached .
$ git add .
```

The first command removes every file from the index. The second re-adds them, but this time Git respects your `.gitignore` rules, so any ignored files are left out. Then commit the result:

```bash
$ git commit -m "Remove ignored files from tracking"
```

## Preview before removing (dry run)

Not sure what will be affected? Use `--dry-run` to see what would be removed without actually doing anything:

```bash
$ git rm -r --cached --dry-run .
rm 'config/database.yml'
rm 'logs/debug.log'
rm 'logs/error.log'
rm '.env'
```

Check this list carefully before running the real command.

## Warning about collaborators

When you remove a file from tracking and push that change, **the file gets deleted for anyone who pulls**. Git sees the removal from the index as a deletion.

If the file contains environment-specific configuration (like `.env` or database credentials), make sure your team knows to back up their local copies before pulling. A heads-up in a commit message goes a long way.

## Commit and push

After removing files from tracking, commit and push as normal:

```bash
$ git status
Changes to be committed:
  deleted:    config/database.yml
  deleted:    logs/debug.log

$ git commit -m "Stop tracking files now in .gitignore"
$ git push
```

For more background on ignore rules, see the [ignoring files guide](/git/ignoring-files). Related commands: [`git commit`](/git/committing-file-changes), [`git status`](/git/commands/status), and [`git add`](/git/commands/add).

---

[DeployHQ](https://deployhq.com) deploys only the files Git tracks, so cleaning up your index means cleaner deployments too. [Try it free](https://deployhq.com/signup).
