Remember that awkward moment when you forgot to exempt some files from being version controlled before doing a git commit? Maybe you committed something like the node_modules folder, your .env file which bears confidential data such as API-keys or database login credentials or you committed User-specific files such as an IDEA configuration file and didn't know how to remove these accidentally committed files from by version control. If this was ever you, this post is for you.
Like is the case with most files you might want to remove it from version control but not delete them from your local computer because these files are still useful. The good news is Git provides a quick and easy to do this .So how is this done? We will break this into five short steps.
- Create a
.gitignore
file that's if you that have one in your project folder already. - Add the path to the folder or file you want to ignore to the
.gitignore
file. - Run the following command:
git rm --cached path/to/file
. Git will list the files it has deleted. The--cached
flag should be used if you want to keep the local copy but remove it from the repository. - Verify that the files have been removed from version control using the
git status
command. - You can now do commits and push changes to the remote repository.
After completing these steps, those files should be removed from version control but not deleted from any local computer. Any other developer who pulls again after you pushed your changes should have no issues. So that's -- simple and plain πππ.
Top comments (4)
Hi Brandon - thank you for this article. I have a question about what I'm seeing when I see this: "fatal: not removing './coverage' recursively without -r"
Hi Erik !
I may be wrong here, but it looks like it says
./coverage
is a folder containing one or more elements, and thus, you need to provide the-r
option, for recursive maybe ?, so that Git shall delete the folder with any elements in it... π€Hope that helps ! Have a nice weekend β
(PS: I'm not a native english speaker, but I'm doing my best. Don't hesistate to tell me if I'm not extra clear... π₯΄)
That's exactly what was going on. :) Thank you! (Your english is great by the way! :) )
Hi ! And thank you for this article, It has been of a great help I must say ! β