How to Quickly Remove A Committed File From Git Version Control
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
.gitignorefile 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
- Run the following command:
git rm --cached path/to/file. Git will list the files it has deleted. The
--cachedflag 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
- 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 😜😜😜.