DEV Community

Cover image for My .gitignore doesn't work, git not ignoring my files/folders. How to fix it ? How to ignore committed files/folders ?
Vic-rider
Vic-rider

Posted on • Edited on

My .gitignore doesn't work, git not ignoring my files/folders. How to fix it ? How to ignore committed files/folders ?

One thing you have to know is that Git only ignores the files/folders that have not been committed to the repository yet and are listed in .gitignore. So before your first commit, remember to set the files/folders you want to ignore to .gitignore and with their right path.

Cause :

  • When you don’t put the right path of your files in .gitignore list
  • When you don’t set your .gitignore list before your first commit. Git don’t ignore the files that are already committed.

Solution :

The solution is quite simple. You have to push again without the file or folder you wanted to exclude existing in your workspace.

A. Remove or Backup Files/Folders

You have to remove all the files/folders that are supposed to be ignored by git and make a commit. But to make things easy, you can back them up to replace them in their right path when the fixing is done.

It will make the files/folders untracked by git. After that, make sure they are properly listed in .gitignore with the right path, and they should no longer be tracked by git.

B. Fixing

  1. Method 01 : More suitable when don’t have many important commits in your repository.

    You have to reset to the previous commit.

  • Run in your project git log.

Image description

  • Run git reset 59c46944f9bef24453a9d9a4cba18eba8c2ed6ba.

  • Add the files/folders you remove or backup.

  • Now you can re-add / re-stage your files with git add -A or git add * and you can check if your files/folders are now ignored by git with git status.

  • After that, you can make a commit with your issue fixed and git ignoring your files/folders. Run git commit -m “commit with the files/folders ignored properly”.

  1. Method 02 :
  • Remove the files from the index (not the actual files in your project) git rm -r –cached ..

  • Add the files/folders you remove or backup.

  • Now you can re-add / re-stage your files with git add -A or git add * and you can check if your files/folders are now ignored by git with git status.

  • After that, you can make a commit with your issue fixed and git ignoring your files/folders. Run git commit -m “commit with the files/folders ignored properly”.

Here is your humble servant Vincent TOSSOU (Vic-rider). I hope this article will help you solve your problem. And if you have any contributions to the content of this article, let me know in the comments.

Thanks.

Top comments (0)