Prevent a deleted file from being tracked in Git

Often it may hap­pen that you need to delete a file such that it should no longer be tracked by GIT. If you sim­ply delete the file from the file sys­tem, git detects that a file has been deleted but it does not get deleted from the com­mit itself sincs its an unstaged change.

To delete a file from your filesys­tem as well inform git of this dele­tion in this com­mit, you can run

git rm filename

But what if you only want to delete the file from the com­mit but retain it in your local direc­tory. You can run

git rm --cached filename

Using this, git will be informed that the file no longer needs to be tracked here­after unless added explic­itly. Any­one who now pulls from this branch will have their local file deleted since git will no longer stores infor­ma­tion about this file from this com­mit onwards.


You may also like...

Leave a Reply

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