DEV Community

Cover image for How to Quickly Remove A Committed File From Git Version Control
Brandon Damue
Brandon Damue

Posted on

4

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.

  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 😜😜😜.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (4)

Collapse
 
erikburdett profile image
Erik Burdett • Edited

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"

Collapse
 
khatastrov profile image
Khatastrov

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... 🥴)

Collapse
 
erikburdett profile image
Erik Burdett

That's exactly what was going on. :) Thank you! (Your english is great by the way! :) )

Collapse
 
khatastrov profile image
Khatastrov

Hi ! And thank you for this article, It has been of a great help I must say ! ✌

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay