.gitignore not working – fixed!

This entry has been copied from the Jonathan Chanon blog.
Although his entry was written several years ago, I have recently started to struggle with GitHub and I’ve run into this problem. So below explains how to resolve it.

This happens to me too often and I always end up googling the answer so this post is probably more of a location I know I can come to find the answer, although by writing it down hopefully it may sink in that I should stop getting too excited on a new project.

New project scenario

You’re all very excited about your new project and you think its about time you committed this to source control. Obviously you’re using Git so you initialise a new repository and commit your files. You then setup a remote repository at Github and it asks you whether you want it create a .gitignore file – you do. So now you have a repository remotely and locally. Easiest thing to do is pull from the remote, setup your remote and push to it. The other scenario might be you’ve committed locally and then realise you need to add a .gitignore file which you do and then commit.

In both cases you will now see the files in your standardised .gitignore file are not being ignored. After a few head scratches you realise its because the .gitignore file should be added to your repo first before any commits.

The solution!

Long story short you have to remove all tracked files and add them back in using the below commands

git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

The first line unstages and removes the paths to your files from the git index recursively.

The second line adds all your files back in but because the .gitignore is present it will not add files that should be ignored!

The final line commits all your files back to the index.

As much as I’d like to take credit for this knowledge I’m going to have to point you in the direction of the stackoverflow post that has helped me in the past.



Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s