Skip to main content
X

Explore your training options in 10 minutes

Gitignore Not Working? A Help Guide.

Felipe Bohórquez - December 29, 2020


The title says it all. There are many cases where you might be troubleshooting the .gitignore file because it is not working as expected.

The .gitignore file plays a crucial role in Git repositories. If you’re .gitignore file is not working, you may accidentally track changes that should be kept private. Some of these changes may contain sensitive information, such as API keys.

Two causes of a broken .gitignore file are:

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

  • Adding a file to .gitignore after you have committed it.
  • Referring to the wrong file name or path in your .gitignore file.

We’ll discuss both of these issues in this tutorial and how to solve them.

What is the .gitignore File?

The .gitignore file contains a list of all the files you do not want to save to your Git repository. This file is often used to keep configuration and environment files outside a repository. These files often contain private information and should not be in any remote repository.

This file should be named and saved explicitly as .gitignore . On this plain text file, you would then add the paths of files or directories you want Git to not track. Whatever is added to this file Git will ignore and not add to its tracking tree.

Here’s an example of a gitignore file contents:

node_modules/
.env

On this file we basically are telling Git to ignore a specific file. This is the .env file, which often contains API keys. We’re also ignoring the node_module folder that contains all installed NPM packages.

Common files to ignore include .DS_Store files, .env files, and config files. .DS_Store files are often found on macOS operating systems and do not need to be part of a Git repository.

Most .gitignore files are placed in the root directory of a repository. To learn more about .gitignore files, check out our .gitignore files guide for beginners .

Scenario A: Adding a File to .gitignore After Committing it

We could complain why gitignore is not working after we mistakenly committed a file that was supposed to be ignored. So we add it to gitignore after that fact and complain that Git is still tracking it.

Well the reason for this is that we need to add files/folders before committing them. Git is already tracking them, so to untrack them again, we will need to do the following command:

git rm -rf --cached <path>

The first part git rm removes the <path>. The –cached flag specifies it is removed from the index but stays in the local directory. r is recursive removal if given a directory path and f forces it. If you are working with just file and still want to keep it on your local do:

git rm --cached <file>

So with this you are indicating Git that you don’t want to track the file/folder provided anymore. Now try again and your gitignore will work as it should . Just remember that the file/folder must be specified for gitignore to work!

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Scenario B: Gitignore Created Before Commit and Still Not Tracking File

If .gitignore is still not tracking the file, the solution might be as simple as checking you are referring to the right file. You need to specify exactly which files you want the .gitignore file to ignore. Any spelling errors or incorrect path names need to be fixed manually by you.

Consider this example:

*.pyyc

The .gitignore file tells Git which files to ignore, which in this case is all .pyyc files. But, our intent is to ignore .pyc files. .pyc files are configuration files for Python programs. Our .gitignore file does not know this so we need to fix our rule:

*.pyc

Our repository will now ignore all .pyc files.

Also check that you don’t have any trailing space at the beginning nor end of file/folder declaration.

Conclusion

We discussed a few scenarios and possibilities of solutions for various cases when gitignore wasn’t working as expected. The main takeaway is that you must understand where the files/directories are located so you can add the correct path.

You must be aware that you should add any files that need to be ignored as soon as you create them. If you happen to have committed it by mistake, luckily we have the git rm -rf –cached <path>. This command will take a file out from the Git workflow. Then, you can add the to gitignore so that your file will be ignored.

Final Note : There might be other cases where the git reset might be useful. I wrote an article regarding git reset that will be useful in these cases. So Git reset will be like undoing, going back in time, so in some cases that is what you want.

Do you want to learn more about Git? Read our How to Learn Git guide . In this guide you’ll find expert advice on learning Git and guidance on top learning resources.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

Felipe Bohórquez

About the author: Felipe Bohórquez is a Software Engineer and technical writer at Career Karma. He covers all things frontend and backend development.

Skip to main content