How to unindex committed files

This post is free for all to read thanks to the investment Mindsers Blog's subscribers have made in our independent publication. If this work is meaningful to you, I invite you to become a subscriber today.

Git is a powerful tool that can seem complicated if we know it badly. In this article, we will see the particular case of bad indexing.

This is a case that can very easily occur when the tool is poorly mastered, but don't panic, there is nothing irreparable. You can make mistakes, correct yourself, start over and learn at your own pace with the trick in this article.

Avoid indexing bad files

There are files we would rather not version with Git. For example, project dependencies (the node_modules directory for a Node.js project), files containing environment variables, or other autogenerated/temporary files.

The .gitignore file allows us to specify to Git the list of files that we do not want it to consider. Create it at the root of your project.

You can specify files, folders or globs there like this:

.env
/node_modules
npm-debug.log*

Once added to this list, the offending files will be invisible to Git. There will no longer be any way to add them accidentally.

Delete file from index

But sometimes we realize that a file should not be indexed only after committing it. An unfortunate git add . or not using git status regularly enough can easily lead to this situation.

If this happens, a simple command exists: git rm.

git rm -r --cached name_of_your_file

Then you just have to commit the deletion of the index:

git add name_of_your_file
git commit

Voilà. The unwanted folder or file should no longer bother you. On the other hand, remember to add it to your .gitignore file so as not to commit it mistakenly again. I refer you to the first part of the article to know how to do it.

Join 100+ developers and entrepreneurs and get notified on every new content.

No spam ever. Unsubscribe in a single click at any time.

If you have any questions or advices, please create a comment below! I'll be really glad to read you. Also if you like this post, don't forget to share it with your friends. It helps a lot!



The comment section is only available for paid members of Mindsers Blog.