Gitignore still commiting the ignored files/folder?
5 min read
Understanding the 'why'
If you are new to Git, and you have just discovered the
.gitignore file. It is a simple implementation. Just add file names or extensions to the file and your files will be omitted. But, it is not always so easy.
What is happening?
So you have set up your project and initialized Git and already halfway through. But now you remember that you forgot to exclude your
node_modules folder in the
.gitignore file. No worries. Just add the
node_modules entry in the
.gitignore file and push it to GitHub. Simple, isn't it?
Well, not quite. If you open GitHub and see the code pane, you can still see the
node_modules directory over there. But, how can this be? Where did you go wrong? Did GitHub mess up? Check your
.gitignore file again, but you will not find anything wrong with it. It still looks like this:
node_modules env logs *.log yarn-debug.log* yarn-error.log* ...
There still is a
node_module entry, which means that you entered it correctly, and the saving process was a success. So what could the issue be? Is
.gitignore being adamant?
Why is this happening?
.gitignore file is doing its job perfectly. The git is at fault here. But don't just start pointing fingers. There must be a very good reason for this.
.gitignorewill prevent untracked files from being added to the set of files tracked by git, however, git will continue to track any files that are already being tracked.
So, in simple words,
.gitignore does not affect the files that are already indexed. Since the
node_modules folder was already added onto the git index on the first commit, it won't run through
.gitignore again. It'll only check for newly created files, not the existing ones, thus, the
node_modules folder will never be removed, if you don't do anything about it.
Now, index, in a way, the git index is like a cache for your project. It is the intermediate level between your local project files and the commits to your repository. To clear this cache, we need to get rid of it, so that project can be staged for the next commit.
Knowing the 'how'
Now that we know why this problem is happening, let's fix it.
Clearing your staged project
To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use:
git rm --cached filename
This is useful when you have just added one file to the
To untrack every file that is now in your .gitignore: First, commit any outstanding code changes, and then, run this command:
git rm -r --cached .
This removes any changed files from the index (staging area). This comes handy when you have added more than one file to the
Hope you found this article informative and intriguing. If it helped you, do give it a like 🧡 and share it with people who might find it useful. Also check out my Instagram and Facebook pages for more content.
Did you find this article valuable?
Support Yash Aryan by becoming a sponsor. Any amount is appreciated!