paint-brush
Learn How to Stop Tracking Files After Adding It to .gitignore in 4 Stepsby@smpnjn
3,810 reads
3,810 reads

Learn How to Stop Tracking Files After Adding It to .gitignore in 4 Steps

by Johnny SimpsonMay 15th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

When we track a file in git, it can sometimes get cached and remain tracked, even if we add it to our .gitignore file. Fortunately, there is an easy way to fix this. git has a built-in rm function which lets us remove cached or tracked changes.
featured image - Learn How to Stop Tracking Files After Adding It to .gitignore in 4 Steps
Johnny Simpson HackerNoon profile picture


When we track a file in git, it can sometimes get cached and remain tracked, even if we add it to our .gitignore file. This is simply because .gitignore prevents files from being added to Git's tracking system, but it will not actively remove those that are already tracked.


This can lead to issues when you have something you no longer want to be tracked, but can't seem to remove from your git repository.


Fortunately, there is an easy way to fix this. git has a built-in rm function which lets us remove cached or tracked changes. To run it, you can use the following command to remove a specific file, where [filename] can be removed with the file you wish to stop tracking:


git rm --cached [filename]


Similarly, if an entire directory needs to be removed, use the -r flag which means recursive, to remove an entire directory and everything within it from tracking:


git rm -r --cached [directory]


After running this command, you can then add amend your commit and push it to your remote:

git add .
git commit -m "Removed tracked files which shouldn't be tracked"
git push


NOTE: This will not remove files from your local computer, but it will remove the tracking of files from your git repository.


It will also remove files from other developers, computers, or servers upon your next git pull.


Be careful with this command!


Also Published Here