Fix: Gitignore is not Working

GitHub has emerged to be a leading pioneer in the field of code collaboration and repository sharing. GitHub is primarily a version control software which allows users control of distributed version control and SCM (source code management). This platform is used by major businesses and companies all around the world.


Platforms like these have their technicalities and issues. One specific problem which the coders experienced was that .gitignore was not working in GitHub. The platform either ignored the .gitignore or worked partially. It is interesting to note that the problem might be a little different in each case since each case is a completely different scenario. However, the solutions which we list will contain fixes which will work universally.

What is .gitignore?

Git (or GitHub) sees every file in your working directory. It characterizes each file as one of the three:

  • Tracked: These files are either committed or staged previously in history.
  • Untracked: These are the files which haven’t been previously staged or committed.
  • Ignored: These are the files which the user himself told Git to ignore completely.

These ignored files might vary scenario to scenario and are mostly machine generated files or build artifacts. This is the common practice; you might be ignoring various other files according to your own needs. Some examples of these files are:

  • Compiled code: These files are usually with the extension .class, .pyc, .ccp etc.
  • Hidden system files: These are files which are used by the system for its operations but are hidden from plain view, for example, DS_Store or Thumbs.db, etc.
  • Build output directories: These are mostly of the directories of /bin, /out, etc.
  • Dependency caches: These files might be the contents of /node or /packages modules.
  • IDE Configuration files: These are config files which are mostly created or managed by your IDE software.
  • Files generated at runtime: There are some programs which create files at run time. If either such code is run, some files might be generated at run time in your working folder and you might ignore them in Git.

Whichever file you wish to ignore is tracked in a special file named as .gitignore which is mostly checked in the root of your working repository. According to the official documentation of GitHub on the subject, there is no specific gitignore command. Instead, you have to manually edit the file which you wish to ignore. The .gitignore file contains patterns which are matched against the file names in your working repository and these are used to help determine whether a specific file should be ignored or not.

What causes .gitignore not to Work?

The .gitignore feature might be working perfectly but you might not have configured it properly. In all of our surveys, we came to the conclusion that the module was indeed working. The reason why coders are unable to use the feature is mostly because they haven’t properly configured the file or there are some conditions which are not being met in the underlying code.

Here are some solutions which might work for you. Each solution might not be applicable in your case so make sure that you switch to the next one if the initial conditions are not being met.

Solution 1: Checking .gitignore File

An interesting case came forth where we saw that the .gitignore file was created in the wrong format. This particular issue occurred when users created the file using the default application of Notepad in Windows OS. It turns out that Notepad writes the file in Unicode instead of ANSI format. In this solution, we will save the changes of Notepad in the correct format of .gitignore and see if this fixes the problem.

Note: You have to remove the extension of .txt from the file when you create a new file using the Notepad.

  1. After writing the code or changes in a new text document in Notepad, click on File and select Save As.
Save As – Notepad
  1. Now in front of Encoding, select ANSI. Now remove the file extension of .txt and save the file with the name ‘.gitignore’. Select the correct directory and save.
Selecting ANSI as Encoding type
  1. Now navigate to the directory and check if the correct file is created. Now test it again with Git and see if the Ignore feature is working as expected.

Developers should abstain from using the default Notepad in Windows. Instead, you should use proper ‘programmers’ notepad. Some examples include Notepad++ etc. In those, you will not have issues like these.

Note: If your file is already saved in UNICODE format, you should save the contents in ANSI format properly if you want your file to be detected properly by Git.

Solution 2: Checking the File you are trying to Ignore

Another condition on which .gitignore works is that your file should not be part of the repository yet. This is a very essential aspect as if this is true, the file will not be ignored as it is already added to the repository. Git cannot ignore that even if you place its name or rule in the .gitignore file. So in essence, Git only ignores untracked files.

You should look at your structure (repository) and make sure that the file which you are currently trying to ignore is not added to the repository. If it is, you would have to remove the file from the repository and after the latest changes are committed, add its name to .gitignore (you can also copy the contents of the file and after deleting it, replicate it with a different name).

Solution 3: Re-adding Files to Repository

If you have already added rules into .gitignore but the files that you want to ignore are already added, we can re-add the files. Adding means that we will remove everything from Git’s index and then add everything back into your repository again. When we add files again from scratch, the rules will be kept in mind which you have added in .gitignore and only the correct files are added.

Note: You should back up your code somewhere else as well before performing this solution. It is always good to have a backup just in case.

  1. Execute the following command. This will unstage and remove the paths to your files from the git index in a recursive manner.
git rm -r --cached .
  1. After this is executed, you should execute the following command. This will add all of your files back and since .gitignore will have the rules, only the correct files will be updated.
git add .
  1. Now we will commit all your files back into the index using the code below:
git commit -m ".gitignore is now working"

Now inspect your files and see if the issue is resolved and you can use .gitignore again without any issues.


Kevin Arrows

Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.