DEV Community

Nathanaël CHERRIER
Nathanaël CHERRIER

Posted on • Originally published at mindsers.blog on

How to unindex committed files

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.

⚠️ Read more of my blog posts about tech and business on my personal blog! ⚠️

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*
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

git add name_of_your_file
git commit
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)