How to make Git “forget” about a file that was tracked

Explanation

Here we will see how can one make Git “forget” about a file that was tracked but is now in .gitignore.

.gitignore is used to prevent the files which are untracked. And also prevent the files to get added (without an add -f) to the set of files that are tracked by Git. However, Git will always continue to track any file that is already being tracked. 

And if you want to stop tracking your file. You have to do is you need to remove it from the index. And this can be done if we use the below command. 

git rm --cached <file>

 So if you don’t want to remove a specific file. But want to remove a whole folder at that time you can use the following command. 

git rm -r --cached <folder>

Such that the removal of the file from the head revision will only happen on the next commit.

WARNING: While this will not remove the physical file from your local, it will remove the files from other developers machines on next git pull.

So this is how we can make Git “forget” about a file that was tracked.

 

Also read, Entity relationship diagram with only crow’s foot notation

Share this post

Leave a Reply

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