Hot to remove tracked file from Git
It has happened to the best of us that we add something to git by mistake, and even commit it (test result folder, log files, etc) but…
How to remove tracked file from Git?
Image from dynamicuser.net
It has happened to the best of us that we add something to git by mistake, and even commit it (test result folder, log files, etc) but then we realized we shouldn’t have done that.
Next move is to add it to “.gitignore” file and then if it changed, you won’t need to commit those changes.
However, that file keeps showing up in git status after it’s edited.
NOTE: While this will not remove the file from your local disk, it will remove the files from other developers machines on next git pull.
Adding the file to .gitignore file will prevent this file from being added (without an add -f) to the set of files tracked by git, however git will continue to track any files that are already being tracked.
To stop tracking a file you need to remove it from the index. You can do that by the following command:
git rm --cached <file>
This command can be used to remove a folder as well like the following
git rm -r --cached <folder>
The removal of the file from the head will happen when you do your next commit.
If you use GIT regularly, I recommend to get this book and have it on your desk:
If you liked this article, click the👏 below so other people will see it here on Medium.
Let’s be friends on Twitter. Happy Coding :)
Cheers!
