This article will explain how to fix files still being tracked by Git after it has been added to the
Rules added to the
.gitignore file only applies to files not yet tracked by Git. If the file was already tracked before adding to the
.gitignore file then it will not be ignored by Git until you untrack the file. This can be done using the following command…
git rm --cached <filename>
This command will remove
<filename> from your git repository but not your filesystem, meaning the files will remain on your disk; although, to Git, the files will seem as though they have been removed. The files will be untracked from Git but the file will remain on your disk as normal – it will not delete your file.
Using the Command with Directories
If using this command and the
<filename> argument is a directory, then the
-r flag must also be used.
node_modules directory was included in a previous commit, now it needs to be untracked and removed from the Git repository.
node_modules has been added to the
.gitignore file. The command to use would be:
git rm --cached -r node_modules
A commit should also be made with this change if desired. E.g.
git commit -m "Remove node_modules from repo"
Untracked – the file is new to the Git repository.
Staged – the Git repo. knows about the file and it will be part of the next commit batch which is called the index.
Unchanged – the file has not changed since the previous commit.
Unstaged – the file has been modified but not yet part of the next commit batch (index).