How to make Git forget a tracked file now in .gitignore

Once we monitor a file in git, it will possibly typically get cached and stay tracked, even when we add it to our .gitignore file. That is just because .gitignore prevents information from being added to Git’s monitoring system, but it surely won’t actively take away these which can be already tracked. This will result in points when you could have one thing you not wish to be tracked, however can not seem to take away it out of your git repository.

Happily, there’s a simple approach to repair this. git has a in-built rm operate which lets us take away cached or tracked adjustments. To run it, you need to use the next command to take away a selected file, the place [filename] will be eliminated with the file you want to cease monitoring:

git rm --cached [filename]
Enter fullscreen mode

Exit fullscreen mode

Equally, if a complete listing must be eliminated, use the -r flag which suggests recursive, to take away a complete listing and all the things inside it from monitoring:

git rm -r --cached [directory]
Enter fullscreen mode

Exit fullscreen mode

After working this command, you’ll be able to then add ammend your commit and push it to your distant:

git add .
git commit -m "Eliminated tracked information which should not be tracked"
git push
Enter fullscreen mode

Exit fullscreen mode

NOTE this won’t take away information out of your native laptop, however it would take away monitoring of information out of your git repository. It can additionally take away the information from different builders computer systems or servers upon your subsequent git pull. Watch out with this command, as I by chance deleted all pictures on FJOLT utilizing it a few weeks in the past. 🙂

Add a Comment

Your email address will not be published. Required fields are marked *