How to Quickly Remove A Committed File From Git Version Control

How to Quickly Remove A Committed File From Git Version Control

ยท

2 min read

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.

  1. Create a .gitignore file that's if you that have one in your project folder already.
  2. Add the path to the folder or file you want to ignore to the .gitignore file.
  3. 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.
  4. Verify that the files have been removed from version control using the git status command.
  5. 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 ๐Ÿ˜œ๐Ÿ˜œ๐Ÿ˜œ.