Git is a powerful version control system that tracks changes to files within a repository.
However, there are instances when you want to exclude certain files from being tracked, even if they were previously added to Git.
In this article, we will explore different solutions to make Git forget about a file that was previously tracked but is now listed in the .gitignore file.
By following these steps, you can ensure that the file remains untracked and unaffected by future Git operations.
Remove the File from Git Cache
The first step is to remove the file from Git’s cache. This will allow Git to recognize the changes made to the .gitignore file. Use the following command to remove the file from the cache:
Example:
$ git rm --cached <file-path>
Replace <file-path>
with the actual path to the file you want to remove from tracking.
Commit the Changes
After removing the file from the cache, commit the changes to the repository. This step ensures that the removal of the file is recorded in Git’s history.
Example:
$ git commit -m "Stop tracking <file-name>"
Replace <file-name>
with the name of the file you removed from tracking.
Verify the File Status
To confirm that the file is no longer being tracked by Git, you can use the following command to check the file status:
Example:
$ git status
If the file is correctly excluded from tracking, it should appear as an untracked file.
Clean Git Index (Optional)
In some cases, Git may still recognize the file due to it being previously tracked. To completely remove the file from Git’s history, you can clean the Git index using the following command:
Example:
$ git update-index --assume-unchanged <file-path>
Replace <file-path>
with the path to the file you want to exclude.
Verify the Exclusion
To ensure that the file is truly ignored by Git, make some changes to the file and then perform a Git operation, such as a status check or commit. If Git doesn’t recognize any changes or prompt you to commit the file, it means Git has successfully forgotten about the file.
Similar Frequently Asked Questions
How do I remove an already tracked file from git?
To remove a file from Git, you need to eliminate it from your tracked files by removing it from the staging area and then committing the changes.
This can be achieved using the git rm
command, which not only removes the file from the staging area but also deletes it from your working directory.
Consequently, the file will no longer appear as an untracked file in subsequent operations.
Consider the following code examples to illustrate this process:
$ git rm <file-path>
Replace <file-path>
with the actual path to the file you want to remove from tracking.
This command removes the file from the staging area and deletes it from the working directory.
$ git commit -m "Remove <file-name>"
Replace <file-name>
with the name of the file you removed.
This command commits the changes and records the removal of the file in Git’s history.
By utilizing the git rm
command followed by a commit, you can effectively remove a file from Git’s tracked files, ensuring it no longer appears as an untracked file in subsequent operations.
How do I ignore a tracked file in git without deleting it?
To ignore a tracked file in Git without deleting it, you can follow these steps:
- Update the
.gitignore
file:
- Open the
.gitignore
file in your project’s root directory using a text editor. - Add an entry for the file you want to ignore. Make sure to use the correct file path relative to the repository’s root directory.
Example .gitignore
:
# Ignore tracked file
path/to/file.txt
- Remove the file from the Git index:
- Use the following command to remove the file from the Git index while preserving it in your working directory:
$ git rm --cached path/to/file.txt
- Save changes and commit:
- Save the changes to the
.gitignore
file. - Commit the changes to record the updated
.gitignore
file and the removal of the file from the Git index.
$ git commit -m "Ignore path/to/file.txt"
By following these steps, you can ignore a tracked file in Git without deleting it.
The file will remain in your working directory but will be excluded from future Git operations, allowing you to keep it in your local environment without it being tracked by Git.
How to untrack files in gitignore?
To untrack files that are listed in the .gitignore
file, you can follow these steps:
- Update the
.gitignore
file:
- Open the
.gitignore
file in your project’s root directory using a text editor. - Remove or comment out the entry for the file you want to untrack.
Example .gitignore
:
# Ignore tracked file
# path/to/file.txt
- Clear the Git index:
- Use the following command to clear the Git index, which will remove all tracked files from the index:
$ git rm -r --cached .
- Re-add and commit the files:
- Add all files back to the index, including the ones previously ignored, using the following command:
$ git add .
- Commit the changes to record the updated index.
$ git commit -m "Untrack files in .gitignore"
By following these steps, you can untrack files that are listed in the .gitignore
file.
The files will no longer be included in future Git operations, allowing you to exclude them from version control while keeping them in your local working directory.
Why is a file in Git still tracked?
When dealing with files that are already tracked in Git but should be ignored according to the .gitignore
file, it’s important to understand that the .gitignore
file only prevents new files from being added to Git’s tracking system.
However, it doesn’t actively remove files that are already tracked.
This can create complications when you want to exclude certain files that are already in your Git repository but no longer want to track them.
In such cases, you can use the following commands to address the issue:
$ git rm --cached <file-path>
The git rm --cached
command removes the specified <file-path>
from the Git index, but it leaves the file intact in your working directory.
This ensures that the file is no longer tracked by Git, while preserving it locally.
$ git commit -m "Stop tracking <file-name>"
By committing the changes with an appropriate message using the git commit
command, you record the removal of the file from Git’s tracking system.
By utilizing these commands, you can effectively address the situation where you have files that you no longer want to be tracked, but are still present in your Git repository.
Wrapping up
By following the steps outlined in this article, you can make Git forget about a file that was previously tracked but is now listed in the .gitignore file.
Remember to remove the file from Git’s cache, commit the changes, and verify the file’s status. Optionally, you can clean the Git index for a complete exclusion.
With these techniques, you can maintain a clean and organized repository that accurately reflects your project’s requirements.
Happy coding with Git!

Abhinav worked as a software engineer at numerous startups and large enterprises for over 12 years. He has worked on a variety of projects, from developing software to designing hardware. He is passionate about tinkering with computers and learning new things. He is always looking for new ways to use technology to solve problems and make people’s lives easier. That is the inspiration behind https://foxrunsoftware.net. Abhinav created FoxRunSoftware to address common errors and issues faced by engineers and non-engineers alike!