How do I make Git forget about a file that was tracked, but is now in .gitignore?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
To make Git forget about a file that was previously tracked but is now listed in .gitignore, you need to remove it from the index (the staging area). Here’s a step-by-step guide to do this:
1. Ensure the file is listed in .gitignore:
- Open your
.gitignorefile and add the file or pattern you want Git to ignore. - Example: To ignore a file named
config.json, add the following line to your.gitignore:
2. Remove the file from the index without deleting it from your working directory:
- Use the
git rm --cachedcommand to remove the file from the index. This tells Git to stop tracking the file, but it will not delete the file from your local filesystem. - Example:
3. Commit the change:
- Commit the change to update the repository.
- Example:
Full Example
Suppose you have a file named config.json that you want to stop tracking:
1. Add config.json to your .gitignore file:
2. Remove the file from the index:
3. Commit the change:
After these steps, config.json will be ignored by Git and will no longer be tracked. Any future changes to this file will not be included in commits.

